Two Methods for configuring interceptor in springmvc, springmvc
Springmvc can be configured with an interceptor in two ways.
First, implement the HandlerInterceptor interface, as shown in figure
Public class MyInterceptor1 implements HandlerInterceptor {// This method is executed before the action is executed to implement data preprocessing, // such as encoding and security control. If the return value is true, the action is executed. @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System. out. println ("executed before MyInterceptor1 action !!! "); Return true; // continue to execute action} // This method is executed after the action is executed and before the view is generated. Here, we have the opportunity to modify the view layer data. @ Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System. out. println ("MyInterceptor1 action") is executed before the view is generated !! ");} // The final execution is usually used to release resources and handle exceptions. We can handle related exceptions based on whether ex is null. // When we handle exceptions at ordinary times, the exception is thrown from the underlying layer, and finally to the spring framework to this method. @ Override public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System. out. println ("MyInterceptor1 last executed !!! It is generally used to release resources !! ");}}
The second is the extendsHandlerInterceptorAdapter class, such
Public class MyInterceptor2 extends HandlerInterceptorAdapter {@ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System. out. println ("MyInterceptor2.preHandle ()"); return true; // continue to execute action }}
The interception of controller URLs can be divided into the following three situations:
Intercept all URLs
For all URLs, specify the url to allow
For all URLs, export turls to external rows
For example, our common LoginInterceptor
Method: inject an attribute List parameter turls into the interceptor.
/User/login
/User/register
LoginInterceptor class
Public class LoginInterceptor extends HandlerInterceptorAdapter {private List
ExceptUrls; public List
Getaskturls () {return response turls;} public void setmediaturls (List
Invalid turls) {this. required turls = required turls;} // run @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String requestUri = request before the action is executed. getRequestURI (); if (requestUri. startsWith (request. getContextPath () {requestUri = requestUri. substring (request. getContextPath (). length (), requestUri. length ();} // system root directory if (StringUtils. equals ("/", requestUri) {return true;} // release url for (String url: Required turls) {if (url. endsWith ("/**") {if (requestUri. startsWith (url. substring (0, url. length ()-3) {return true;} else if (requestUri. startsWith (url) {return true ;}// other url String sessionid = request. getSession (). getId (); UserSessionStatus status = SessionManager. getStatus (sessionid); // if you have not logged on to if (null = status) {// return to the logon page return false;} else {return true ;}}}