Summary of springMVC interceptors and filters

  • 2020-05-30 20:16:04
  • OfStack

Interceptor: used to intercept access to url

Use: permission verification, messy code Settings and so on

Configuration in spring-mvc.xml file:


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
            http://www.springframework.org/schema/tx
            ">


  <!-- Write interceptor -->
  <mvc:interceptors>
    <!-- For a particular url intercept -->
    <mvc:interceptor>
      <mvc:mapping path="/test.do"/>
      <bean class="com.hbut.interceptor.TestInterceptor"/>
    </mvc:interceptor>
    <mvc:interceptor>
      <!-- Intercepting a specific module 1 Level to intercept -->
      <mvc:mapping path="/test/ x /"/>
      <bean class="com.hbut.interceptor.TestInterceptor1"/>
    </mvc:interceptor>

    <mvc:interceptor>
      <!-- Intercepting specific modules -->
      <mvc:mapping path="/test/ x "/>
      <bean class="com.hbut.interceptor.TestInterceptor2"/>
    </mvc:interceptor>

  </mvc:interceptors>

Intercept all url


 <mvc:interceptors>
   <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
 </mvc:interceptors>

java code


package com.hbut.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author XiJun.Gong
 * @DATE 2016/6/1.
 * aim:  com.hbut.interceptor
 */
public class TestInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    
   //todo  Add the action to be done here code 
   System.out.println("preHandle"); 
    return true; //todo  In this case false The request will not arrive control layer 
  }

  @Override
  public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    System.out.println("postHandle"); //todo  Can be used to modify information, jumps, etc 
  }

  @Override
  public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    System.out.println("afterCompletion"); //todo  The last execution 
  }
}

Another interceptor: much the same


package com.hbut.interceptor;

import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

/**
 * @Author XiJun.Gong
 * @DATE 2016/6/1.
 * aim:  com.hbut.interceptor
 */
public class Test2Interceptor implements WebRequestInterceptor {
  @Override
  public void preHandle(WebRequest webRequest) throws Exception {
  }

  @Override
  public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {

  }

  @Override
  public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {

  }
}

Filter: relies on servlet container, USES callback function, filter range is large

Interceptors: flexible and dependent on framework containers such as spring and mybatis


Related articles: