java spring mvc of the interceptor

1. springmvc interceptors is realized by HandlerInterceptor or inheritance HandlerInterceptorAdapter achieved.

2. a custom implementation interceptor steps of:

a). HandlerInterceptor define a class that implements interface

public  class myInterceptor the implements HandlerInterceptor {
     / ** 
     * before processing execution method , generally used to do some preparatory work: such as logging, permission checks 
     * If it returns false representation intercepted, will not perform the processing method 
     * returns true to continue processing method 
     * / 
    @Override 
    public  Boolean  The preHandle (the HttpServletRequest REQ, the HttpServletResponse RESP, Object Handler) throws Exception { 
        System.out.println ( "--------- performed preHandler" req.getRemoteHost + () + req.getRemoteUser ( )); 
        resp.sendRedirect ( "the index.jsp" );
         return  to false ; 
    } 
    / ** 
     *After processing method executed before execution rendered views is generally used to do some cleanup
      * / 
    @Override 
    public  void The postHandle (the HttpServletRequest REQ, the HttpServletResponse RESP, Object Handler, ModelAndView Music Videos)
             throws Exception { 
        System.out.println ( " performing PostHandler " ); 
    } 
    / ** 
     * performed to release resources after general view rendering
      * / 
    @Override 
    public  void afterCompletion (the HttpServletRequest the arg0, the HttpServletResponse arg1, arg2 Object, Exception arg3)
             throws Exception { 
        System.out.println ( " execution afterCompletion " ); 
    }
}

b) In springmvc profile, add an interceptor arranged

<MVC: interceptors> 
        <-! define a configuration interceptor -> 
        < MVC: Interceptor > 
            ! <- which specifies intercepted url mapping 
                 / * represents all requests in the root path is intercepted - / hello.do 
                / ** indicates that all requests in the root path and sub path is intercepted /user/add.do 
            -> 
            <MVC: Mapping path = "/ **" /> 
            ! <- configuration interceptor path -> 
            < class = the bean "cn.sxt.interceptor.MyInterceptor"> </ the bean> 
        </ MVC: Interceptor >
    </ MVC: interceptors>

c) Test

3. Log interceptors to achieve

public  class  LoginInterceptor  the extends the cunningly named HandlerInterceptorAdapter { 
    @Override 
    public  Boolean The preHandle (the HttpServletRequest Request, Response the HttpServletResponse, Object Handler)
             throws Exception { 
        
        // if the address verification request is logged if the address is performed to continue down the 
        String URI = Request.getRequestURI (); 
        URI uri.substring = (uri.lastIndexOf ( "/") +. 1 );
         if (uri.equals ( "login.do" )) {
             return  to true ; 
        } 
        // verify whether a user is present if the session continues 
        if(.! Request.getSession () the getAttribute ( "username") = null ) {
             return  to true ; 
        } 
        // execution to jump to the login page 
        Response.sendRedirect (request.getContextPath () + "/ the login.jsp" );
         return  to false ; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11280240.html