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