Two Methods for configuring interceptor in springmvc, springmvc

Source: Internet
Author: User

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 ;}}}
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.