A Brief analysis of the role and implementation of Spring MVC interceptor in java

  • 2020-07-21 07:42:24
  • OfStack

Implementation of the interceptor

1. Write interceptor class to implement HandlerInterceptor interface;

2. Register interceptors in THE springmvc framework;

3. Configure the intercepting rules of the interceptor;

Other implementation methods

WebRequestInterceptor interface:

The difference from the previous one is that the parameter difference and prehandle's method do not return a value. The previous one is not fully functional, so the first one is commonly used.

Use scenarios for interceptors

Handle all request common issues:

1. Messy code: use request and response parameters to set the code;

2. Solve the authentication problem (login or not, take session object to view);

The difference between interceptors and filters

1. The interceptor Interceptor relies on the framework container and only filters requests based on reflection mechanism;

2. Filter Filter is dependent on Servlet container, based on callback function, and has a large filtering range;

example


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
public class TestInterceptor implements HandlerInterceptor {
 /**
  *  The return value indicates whether the current request needs to be intercepted 
  * false : The request will be terminated 
  * true : The request will continue to run 
  * Object handler  Represents the target object of the intercepted request 
  */
 @Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
   throws Exception {
  System.out.println("TestInterceptor.preHandle()");
  return true;
 }
 /**
  * modelAndView : 
  *   Can be achieved by modelAndView Parameter changes the view displayed, or modifies the method sent to the view 
  */
 @Override
 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
   ModelAndView modelAndView) throws Exception {
  System.out.println("TestInterceptor.postHandle()"); 
 }
 /**
  *  Called after the view has been loaded 
  *  Often used in the destruction of resources, flow, etc 
  *  It's like a destructor 
  */
 @Override
 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
   throws Exception {
  System.out.println("TestInterceptor.afterCompletion()"); 
 }
}

Registration:

Add interceptor code to springmvc's Dispatcherservlet configuration file;


<!--  Registered interceptor  -->
 <mvc:interceptors>
 <!--  Single interceptor : Set the specific intercept path  -->
 <mvc:interceptor>
  <mvc:mapping path="/courses/view2/*">
  <bean class="com.cc.mvcdemo.interceptor.TestInterceptor"></bean>
 </mvc:mapping></mvc:interceptor>
 <!--  The first 2 Way: Intercept passing through the current Dispatcherservlet All requests  -->
 <bean class="com.cc.mvcdemo.interceptor.TestInterceptorTwo"></bean>
 </mvc:interceptors>

Hope this article content is helpful to each friend


Related articles: