Tags-get the request in the custom spring interceptor by request to the target method object in controller _web development

Source: Internet
Author: User
Tags aop
Why to read the method object. Of course we can do a lot of things, such as authorization, to determine whether the user has access to the method's permissions.
Spring-webmvc-3.1.0.release and above version
Final configuration--------------------------------------------begin <bean Class= " Org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator "/> <bean class=" Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/> <bean class=" Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "> <property name=" Interceptors "> <list> <bean class=" Com.chenzhou.examples.erm.util.interceptor.LoginInterceptor "/> </list> </property> </bean> <bean class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "/>
Spring's own interceptor, test public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler) t Hrows Exception {System.out.println ("*********************prehandle********************"); System.out.println (Handler.getclass ()); Handlermethod Handlermethod = (handlermethod) handler; System.out.println (Handlermethod.getmethod ()); return true; }
Final configuration--------------------------------------------End


Possible Errors *********************prehandle********************   class  com.chenzhou.examples.erm.web.admincontroller   2012-10-21 16:28:25  org.apache.catalina.core.standardwrappervalve invoke   serious:  servlet.service ()  for  servlet erm threw exception   java.lang.classcastexception:  com.chenzhou.examples.erm.web.admincontroller cannot be cast to  org.springframework.web.method.handlermethod       at  Com.chenzhou.examples.erm.util.interceptor.LoginInterceptor.preHandle (logininterceptor.java:37)        at org.springframework.web.servlet.dispatcherservlet.dodispatch ( dispatcherservlet.java:891)        at  Org.springframework.web.servlet.DispatcherServlet.doService (dispatcherservlet.java:827)         at org.springframework.web.servlet.frameworkservleT.processrequest (frameworkservlet.java:882)        at  Org.springframework.web.servlet.FrameworkServlet.doGet (frameworkservlet.java:778)         at javax.servlet.http.httpservlet.service (httpservlet.java:617)         at javax.servlet.http.httpservlet.service (httpservlet.java:717)    ......  

According to the error hint can be seen is handlermethod Handlermethod = (handlermethod) handler; This step is an error, according to System.out.println (Handler.getclass ()); The printed result shows that handler is the Controller class that the request accesses and cannot be converted to a Handlermethod object. The solution is to use the

<bean class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" >
Replace
<bean class= "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

Because defaultannotationhandlermapping can only return controller objects, it does not map to the method level in controller. After the replacement, the configuration is as follows:

<bean class= "Org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/> <bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <bean class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "> <property name=" Interceptors "> <list> <bean class=" com.chenzhou.examples.erm.util.interceptor.LoginInt Erceptor "/> </list> </property> </bean>

After restarting the Tomcat test, I found the error again, and reported another mistake with the following details:

2012-10-21 16:39:39 org.apache.catalina.core.StandardWrapperValve Invoke severity: Servlet.service () for Servlet erm threw exc Eption Javax.servlet.ServletException:No Adapter for handler [public Org.springframework.web.servlet.ModelAndView Com.chenzhou.examples.erm.web.AdminController.init (Javax.servlet.http.HttpServletRequest,   Javax.servlet.http.HttpServletResponse)]: Does your handler implement a supported interface like Controller? ......

This time, the request did not reach the interceptor container to have an error, which means that the handler object's adapter class cannot be found. I'm in the Requestmappinghandlermapping class corresponding to the Spring-webmvc-3.1.0.release.jar The adapter class corresponding to the class is found in the package: Requestmappinghandleradapter

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.