ssm framework springMVC interceptor

1 interception Overview

1.1 What is a blocker?

springMVC the interceptor (Interceptor) similar to the filter (Filter) servlet is, it is mainly for intercepting a user request and processed accordingly. For example permissions may be verified by the interceptor, the log recording request information, determines whether the user login and the like.

To use springMVC the interceptor, it is necessary to define and configure the interceptor classes. Typically interceptor class can be defined in two ways.

1 is defined by the implement HandlerInterceptor interface or inherit HandlerInterceptor interface implementation class.

2 is defined by implementing WebRequestInterceptor interface or inherit WebRequestInterceptor interface implementation class.

HandlerInterceptor interface mode in order to achieve, for example, a custom interceptor class code as follows:

public class CustomInterceptor implements HandlerInterceptor{
        public boolean preHandle(HttpServletRequest request, 
                                 HttpServletResponse response, Object handler)throws Exception {
            return false;
        }
        public void postHandle(HttpServletRequest request, 
                               HttpServletResponse response, Object handler,
                               ModelAndView modelAndView) throws Exception {
            
        }
        public void afterCompletion(HttpServletRequest request,
                                    HttpServletResponse response, Object handler,
                                    Exception ex) throws Exception {
        }
    }

The above code, custom interceptor HandlerInterceptor implements interfaces, and implements three interface methods:

The preHandle () Method: The method will be executed before the control method, the interrupt return value indicates whether the subsequent operation. When it returns a value of true, indicating that it goes on; when the return value is false, will break all the subsequent operations (including a method of performing the next call interceptor controller class and the like).

The postHandle () method: This method calls the method after the controller, and a view before analysis execution. We can make further changes to the model and the view through this method.

afterCompletion () method: This method will complete the entire request, that is executed after the end view rendering can be achieved by this method to clean up some resources, logging information and so on.

Guess you like

Origin www.cnblogs.com/liu1275271818/p/11502382.html