HandlerInterceptorAdapter和HandlerInterceptor

1. 简介 

要自定一个拦截器,要门继承 HandlerInterceptorAdapter类 或者实现 HandlerInterceptor接口,第一种方法已经过期。

 

 

2. 

public class LoginInterceptor implements HandlerInterceptor {
    //Intercept the execution of a handler. Called after HandlerMapping determined
    //an appropriate handler object, but before HandlerAdapter invokes the handler.
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getSession().getAttribute("user")==null){
            response.sendRedirect("/admin");
            return false;
        }
        return true;
    }

    //Intercept the execution of a handler. Called after HandlerAdapter actual
    // ly invoked the handler, but before the DispatcherServlet renders the view.
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    //  Callback after completion of request processing
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

 

posted @ 2021-11-21 16:46  木有呂朋友  阅读(652)  评论(0编辑  收藏  举报