Spring MVC's interceptor is at the HandlerMapping level. There can be multiple HandlerMappings. Each HandlerMapping can have its own interceptor. Please learn the details through this article.
Spring provides us with :
org.springframework.web.servlet.HandlerInterceptor interface,
org.springframework.web.servlet.handler.HandlerInterceptorAdapter adapter,
implement this interface or Inheriting this class makes it very convenient to implement your own interceptor.
There are the following three methods:
Executed before Action:
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler);
Execute before generating the view
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView);
Finally executed, which can be used to release resources
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
respectively Implement preprocessing, postprocessing (Service is called and ModelAndView is returned, but the page is not rendered), and return processing (the page has been rendered)
In preHandle, encoding, security control, etc. can be performed;
In postHandle, you have the opportunity to modify the ModelAndView;
In afterCompletion, you can determine whether an exception has occurred and perform logging based on whether ex is null.
The Object handler in the parameter is the next interceptor.
How to use interceptors?
To customize an interceptor, you need to implement the HandlerInterceptor interface:
Java code
##
public class MyInteceptor implements HandlerInterceptor { 略。。。 }
There are three ways to configure it in the spring MVC configuration file:
Option 1, (approximate) total interceptor, intercept all urlsJava Code<mvc:interceptors> <bean class="com.app.mvc.MyInteceptor" /> </mvc:interceptors>
An interceptor will be injected into each HandlerMapping. There is always a HandlerMapping that can find the processor, and at most only one processor can be found, so this interceptor will always be executed. Acts as a total interceptor.
<mvc:interceptors > <mvc:interceptor> <mvc:mapping path="/user/*" /> <!-- /user/* --> <bean class="com.mvc.MyInteceptor"></bean> </mvc:interceptor> </mvc:interceptors>
##
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <bean class="com.mvc.MyInteceptor"></bean> </list> </property> </bean>
it will automatically register DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter These two beans, so there is no chance to inject the interceptors attribute into it, and the interceptor cannot be specified. Of course, we can manually configure the two beans above without using
In fact, I don’t recommend using
but recommend manually writing detailed configuration files instead <mvc: annotation-driven />
, which gives stronger control. How to replace
? What exactly did he do? One sentence
actually did the following work: (excluding adding your own defined interceptor)After we understand this, The control over Spring3 MVC is even stronger, and you can change whatever you want.
Xml code
<!-- 注解请求映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <ref bean="logNDCInteceptor"/> <!-- 日志拦截器,这是你自定义的拦截器 --> <ref bean="myRequestHelperInteceptor"/> <!-- RequestHelper拦截器,这是你自定义的拦截器--> <ref bean="myPermissionsInteceptor"/> <!-- 权限拦截器,这是你自定义的拦截器--> <ref bean="myUserInfoInteceptor"/> <!-- 用户信息拦截器,这是你自定义的拦截器--> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="byteArray_hmc" /> <ref bean="string_hmc" /> <ref bean="resource_hmc" /> <ref bean="source_hmc" /> <ref bean="xmlAwareForm_hmc" /> <ref bean="jaxb2RootElement_hmc" /> <ref bean="jackson_hmc" /> </list> </property> </bean> <bean id="byteArray_hmc" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /><!-- 处理.. --> <bean id="string_hmc" class="org.springframework.http.converter.StringHttpMessageConverter" /><!-- 处理.. --> <bean id="resource_hmc" class="org.springframework.http.converter.ResourceHttpMessageConverter" /><!-- 处理.. --> <bean id="source_hmc" class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /><!-- 处理.. --> <bean id="xmlAwareForm_hmc" class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /><!-- 处理.. --> <bean id="jaxb2RootElement_hmc" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /><!-- 处理.. --> <bean id="jackson_hmc" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /><!-- 处理json-->
The above is the detailed content of Detailed introduction to Spring MVC interceptor. For more information, please follow other related articles on the PHP Chinese website!