The SpringMVC project obtains the Controller ing from all URLs to the Controller Method, springmvcmethod
Spring is a very powerful open-source framework. It is like a container that provides various Bean components and services. For MVC, it implements logic processing from the Url request ing Controller method, in our daily development work, we don't need to pay too much attention to how the Url is mapped to the method in the Controller. Everything is done by Spring MVC.
However, when I was working on the SDK project today, I suddenly came up with the idea: Can I print out the ing information of all URLs and methods in my project? So that I can see at a glance that I have completed the development of those API interfaces, what parameters are required for these methods. As shown in:
Private class RequestToMethodItem {public String controllerName; public String methodName; public String requestType; public String requestUrl; public Class <?> [] MethodParmaTypes; public RequestToMethodItem (String requestUrl, String requestType, String controllerName, String requestMethodName,
Class<?>[] methodParmaTypes) { this.requestUrl = requestUrl; this.requestType = requestType; this.controllerName = controllerName; this.methodName = requestMethodName; this.methodParmaTypes = methodParmaTypes; } }
2. Collect information and create an object.
@ RequestMapping (value = "/index", method = RequestMethod. GET) public ModelAndView index (HttpServletRequest request) {ServletContext servletContext = request. getSession (). getServletContext (); if (servletContext = null) {return null;} WebApplicationContext appContext = WebApplicationContextUtils. getWebApplicationContext (servletContext); // List of mappings between request URLs and processing methods <RequestToMethodItem> requestToMethodItemList = new ArrayList <RequestToMethodItem> (); // obtain all RequestMapping maps <String, handlerMapping> allRequestMappings = BeanFactoryUtils. beansOfTypeIncludingAncestors (appContext,
HandlerMapping. class, true, false); for (HandlerMapping handlerMapping: allRequestMappings. values () {// This project only requires URL if ing in RequestMappingHandlerMapping if (handlerMapping instanceof attributes) {handle = (handle) handlerMapping; Map <RequestMappingInfo, HandlerMethod> handlerMethods = handle. GetHandlerMethods (); for (Map. entry <RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry: handlerMethods. entrySet () {RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry. getKey (); HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry. getValue (); RequestMethodsRequestCondition methodCondition = requestMappingInfo. getMethodsCondition (); String requestType = SetUtils. first (methodCondition. getMethods ()). name (); PatternsRequestCondition patternsCondition = requestMappingInfo. getPatternsCondition (); String requestUrl = SetUtils. first (patternsCondition. getPatterns (); String controllerName = mappingInfoValue. getBeanType (). toString (); String requestMethodName = mappingInfoValue. getMethod (). getName (); Class <?> [] MethodParamTypes = mappingInfoValue. getMethod (). getParameterTypes (); RequestToMethodItem item = new RequestToMethodItem (requestUrl, requestType, controllerName, requestMethodName,
methodParamTypes); requestToMethodItemList.add(item); } break; } } return new ModelAndView("index").addObject("MethodList", requestToMethodItemList); }
In this step, I only use the ing information in "RequestMappingHandlerMapping" here. In fact, there are three other HandlerMapping. If you want to obtain all the ing information in the project, it must be one of them. The debugging information is as follows:
<! DOCTYPE html> The ajax url cannot be sent to the spring mvc controller.
404 is that your controller has not been created!
Check whether the controller has been mapped.
Open the LOG and specify to SPRING
Check whether the MVC print log is mapped to a path such as/park.
1. troubleshooting method
Can I access it in a browser?
2. if it cannot be accessed, It is not mapped.
Net MVC2 project references a class library project. How can I access the Controller/Action in the class library project with a URL?
In a non-MVC project, you cannot use the Controller/Action/Param method in the URL, the reason is the features of the MVC Project template (in fact, it is the access parsing or ing method encapsulated by the ASP.net MVC development team) it is determined that only projects using MVC templates can be accessed in the Controller/Action mode.
Solution: Separate the Controller class in the class library project and put it in the contorroller directory of the MVC project. Of course, the class dependency with the original class library is as follows, you can introduce the namespace of the original class library in this class.
This change still requires debugging at 1.1 points. Don't worry about the trouble ~.