Home > Java > javaTutorial > Detailed introduction to Spring MVC interceptor

Detailed introduction to Spring MVC interceptor

巴扎黑
Release: 2017-09-08 09:43:36
Original
1385 people have browsed it

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);
Copy after login

Execute before generating the view


 public void postHandle(HttpServletRequest request,
  HttpServletResponse response, Object handler,
  ModelAndView modelAndView);
Copy after login

Finally executed, which can be used to release resources


 public void afterCompletion(HttpServletRequest request,
  HttpServletResponse response, Object handler, Exception ex)
Copy after login

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 {   
  略。。。 
}
Copy after login

Spring MVC and Without a total interceptor, all requests cannot be intercepted before and after.

Spring MVC's interceptor is at the HandlerMapping level. There can be multiple HandlerMappings, and each HandlerMapping can have its own interceptor.

When a request executes the implementation classes of the HandlerMapping interface sequentially according to the Order value from small to large, whichever returns first, it is over. The subsequent HandlerMapping will not go away, and the process is completed. . Just move on to the next process.

When will the interceptor be executed? When a request is handed over to a HandlerMapping, the HandlerMapping first looks for a processor to handle the request. If it finds it, it executes the interceptor. After executing the interception, it hands it to the target processor.

If the processor is not found, then this interceptor will not be executed.

There are three ways to configure it in the spring MVC configuration file:

Option 1, (approximate) total interceptor, intercept all urls

Java Code


  <mvc:interceptors> 
  <bean class="com.app.mvc.MyInteceptor" /> 
</mvc:interceptors>
Copy after login

Why is it called "approximate"? As mentioned before, Spring does not have a total interceptor.

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.

If it is a REST-style URL, static resources will also be intercepted.


Option 2, (approximate) total interceptor, intercepts matching URLs.

Xml code


<mvc:interceptors >  
 <mvc:interceptor>  
    <mvc:mapping path="/user/*" /> <!-- /user/* -->  
    <bean class="com.mvc.MyInteceptor"></bean>  
  </mvc:interceptor>  
</mvc:interceptors>
Copy after login

has one more URL match than Solution 1.

If it is a REST-style URL, static resources will also be intercepted.


Option 3, interceptor on HandlerMappint.

If it is a REST-style URL, static resources will not be intercepted. Because we injected the interceptor accurately.

Xml code

##

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">    
 <property name="interceptors">    
   <list>    
     <bean class="com.mvc.MyInteceptor"></bean>   
   </list>    
 </property>    
</bean>
Copy after login

If

is used,

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 , and then inject interceptors into the interceptors attribute.


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-->
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template