springMVC use interceptors do Pretreatment methods for control, post-processing, resource cleanup

A quick look at filters and interceptors:

Filters are part of the servlet specification, any of the Java
Web project can be used; after configuration url-pattern of / *, can intercept all the resources you want to access. Interceptor is a part of himself springMVC framework, only use springMVC framework of the project in order to use; interceptor will intercept controller method, namely the method of the Controller, visit jsp, HTML, css, image, js is not to intercept.

springMVC blocker, acting on the front and rear Controller method performed by the implement org.springframework.web.servlet.HandlerInterceptorto implement the interface.
Interface has three methods: preHandleBefore calling on the Controller action method, pretreatment; postHandleacting on the Controller after the method call, after treatment; afterCompletionthe name implies acting on the engine load is complete after the page template, mainly for some of the recycling work.
preHandleMethod returns a value when it returns false, the method will not be executed Controller, will not go into the target page. Method parameters by httpServletRequestforwarding a request to do.

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

The following is a simple realization interceptor steps:

  1. Controller write
    and no difference between the conventional control method, the output of a sequence of statements in the method, the rear view of the execution order associated method according to the printing result of the console.
@Controller
public class TestInterceptorController {
    @RequestMapping("/testInterceptor")
    public String testInterceptor(){
        System.out.println("testInterceptor...");
        return "success";
    }
}
  1. Write blocker
public class TestInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        System.out.println("preHandle...");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("afterCompletion...");
    }
}
  1. Configuring interceptor
    addition Pack scan configuration, annotation drive viewresolver outside, there needs to be configured interceptor to associate the interceptor method, and a controller. If there are a plurality of interceptors be configured in <mvc:interceptors>add the tags plurality <mvc:interceptor>of unit modules can. If a controller method for the same, there are multiple interceptors, it will perform in accordance with the order of configuration.
    <!--配置拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/testInterceptor"/>
            <bean class="com.wuwl.interceptor.TestInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
  1. Write front page
    simple hyperlink to.
    <a href="testInterceptor">testInterceptor</a>
  1. Writing success page
    Here we add that in jsp page background output, you can see the order interceptor execution.
    <p>you success</p>
    <%
        System.out.println("loading jsp success");
    %>
  1. Start tomcat, test demo
  2. View the console output
    Here we can clearly see that after adding interceptors, access /testInterceptorrequest, first came to preHandle method interceptors, preprocessing; after returns true, execution control method, the method returns the string; execution postHandle method interception, do after treatment; simultaneously view parser according to the returned string splicing, point to jump page; the page loads shall then perform afterCompletion method interception, recovered related resources.
preHandle...
testInterceptor...
postHandle...
loading jsp success
afterCompletion...

Note: If there are multiple interceptors to intercept the same request, two demonstrated here, we will output statement interceptors plus number. So that the last console output statement will become:

preHandle111...
preHandle222...
testInterceptor...
postHandle222...
postHandle111...
loading jsp success
afterCompletion222...
afterCompletion111...

postHandleAnd afterCompletionorder and execution of preHandlethe order is reversed, in this order from the original springmvc.xmlconfiguration sequence interceptor.

Published 88 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41885819/article/details/104502546