The SpringMVC project obtains the Controller ing from all URLs to the Controller Method, springmvcmethod

Source: Internet
Author: User

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 ~.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.