Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat:使一个Controller类中支持多个接口方法 #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ private void initHandleMethods() {
for (Controller controller : ServiceLoader.load(Controller.class)) {
Class<?> controllerClass = controller.getClass();
Path pathFromClass = controllerClass.getAnnotation(Path.class);
String requestPath = pathFromClass.value();
String requestPathByClass = pathFromClass.value();
Method[] publicMethods = controllerClass.getMethods();
// 处理方法支持的 HTTP 方法集合
for (Method method : publicMethods) {
Set<String> supportedHttpMethods = findSupportedHttpMethods(method);
Path pathFromMethod = method.getAnnotation(Path.class);
String completeRequestPath = requestPathByClass;
if (pathFromMethod != null) {
requestPath += pathFromMethod.value();
completeRequestPath += pathFromMethod.value();
}
handleMethodInfoMapping.put(requestPath,
new HandlerMethodInfo(requestPath, method, supportedHttpMethods));
handleMethodInfoMapping.put(completeRequestPath, new HandlerMethodInfo(completeRequestPath, method, supportedHttpMethods));
controllersMapping.put(completeRequestPath, controller);
}
controllersMapping.put(requestPath, controller);
}
}

Expand Down Expand Up @@ -134,8 +134,8 @@ public void service(HttpServletRequest request, HttpServletResponse response)
}

if (controller instanceof PageController) {
PageController pageController = PageController.class.cast(controller);
String viewPath = pageController.execute(request, response);
// 调用对应的controller方法
String viewPath = (String) handlerMethodInfo.getHandlerMethod().invoke(controller, request, response);
// 页面请求 forward
// request -> RequestDispatcher forward
// RequestDispatcher requestDispatcher = request.getRequestDispatcher(viewPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public class HelloWorldController implements PageController {
public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
return "index.jsp";
}

@Path("/world2")
public String world2(HttpServletRequest request, HttpServletResponse response) throws Throwable {
return "index.jsp";
}
}