Interceptors are compared to filters, which intercept both control class methods and their static pages, whereas filters intercept only controller methods

Interceptor idea

The interceptor methods correspond to pre-processing, post-processing and post-page execution respectively

1. Write interceptor classes

/** * Custom interceptor */
public class MyInterceptor1 implements HandlerInterceptor{

    /** * preprocessing, the controller method returns true before executing, executes the next interceptor, if not, executes the controller method * return false does not pass */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor1 executes... Before 1111");
        // You can return flase and forward the request to another page, using the request parameter
        // request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request,response);
        return true;
    }

    /** * after the controller method is executed, before the success.jsp is executed */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor1 executes... After 1111");
        // request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request,response);
    }

    /** * after the success.jsp page is executed, the method executes */
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor1 executes... The 1111"); }}Copy the code

The second interceptor is identical to the first interceptor

public class MyInterceptor2 implements HandlerInterceptor{
Copy the code

2. Configure interceptors

MVC. XML file

<! -- Configure interceptor -->
<mvc:interceptors>
    <! -- Configure first interceptor -->
    <mvc:interceptor>
        <! -- The specific method to intercept -->
        <mvc:mapping path="/user/*"/>
        <! < MVC :exclude-mapping path=" /> -->
        <! -- Configure interceptor object -->
        <bean class="cn.itcast.controller.cn.itcast.interceptor.MyInterceptor1" />
    </mvc:interceptor>
    <! Configure the second interceptor -->
        <mvc:interceptor>
            <! -- The specific method to intercept -->
            <mvc:mapping path="/ * *"/>
            <! < MVC :exclude-mapping path=" /> -->
            <! -- Configure interceptor object -->
            <bean class="cn.itcast.controller.cn.itcast.interceptor.MyInterceptor2" />
        </mvc:interceptor>
    </mvc:interceptors>
Copy the code

Controller class file

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/testInterceptor")
    public String testInterceptor(a){
        System.out.println("TestInterceptor executes...");
        return "success"; }}Copy the code

Front end success.jsp page

<% System.out.println("Success. JSP executes..."); % >Copy the code

The final output results are in the same order as the arrows in the schematic diagram. The output results are as follows