zoukankan      html  css  js  c++  java
  • SpringMVC-Filter&Interceptor

    SpringMVC的Filter和Interceptor

    Filter和Interceptor的执行顺序

    1. Filter.init()

    2. Filter.doFilter()之前

    3. HandlerInterceptor.preHandle()

    4. Controller方法执行

    5. HandlerInterceptor.postHandle()

    6. DispatcherServlet视图渲染

    7. HandlerInterceptor.afterCompletion()

    8. Filter.doFilter()之后

    9. Filter.destroy()

    Filter和Interceptor的区别

    1. Filter是基于函数回调(doFilter()方法)的,而Interceptor则是基于Java反射的(AOP思想)。

    2. Filter依赖于Servlet容器,而Interceptor不依赖于Servlet容器。

    3. Filter对几乎所有的请求起作用,而Interceptor只能对action请求起作用。

    4. Interceptor可以访问Action的上下文,值栈里的对象,而Filter不能。

    5. 在action的生命周期里,Interceptor可以被多次调用,Servlet容器在实例化Filter后只调用一次Filter.init()

    The servlet container calls the init method exactly once after instantiating the filter. 

    Filter的使用:

    @WebFilter(filterName = "filterOne", urlPatterns = {"/*"})
    public class FilterOne implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("init");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println("===========before doFilter");
            filterChain.doFilter(servletRequest, servletResponse);
            System.out.println("===========after doFilter");
        }
    
        @Override
        public void destroy() {
            System.out.println("destroy");
        }
    }

    给你的配置类(或者启动类)加上
    @ServletComponentScan("路径")

    或者

    @Configuration
    public class FilterConfig {
        @Bean
        public FilterRegistrationBean filterRegistrationBean(){
            FilterRegistrationBean bean = new FilterRegistrationBean();
            bean.setFilter(new FilterTwo());
            bean.addUrlPatterns("/*");
            return bean;
        }
    }

     Interceptor的使用

    public class MyInterceptor implements HandlerInterceptor {

      // 如果拦截器要使用容器里的bean的话,要先把拦截器注入到容器 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { System.out.println("preHandle"); if (...) {
          return true;
    }
    return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { System.out.println("postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { System.out.println("afterCompletion"); } }
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(currentLimitingInterceptor())
                    .addPathPatterns("/*");
        }
    
    }
  • 相关阅读:
    去掉Form产生的空行
    转:Override错误
    面试
    JMF获取设备列表失败,获取视频设备失败?
    jquery 插件ztree的应用动态加载树节点数据
    关于Struts2上传文件
    未能解析引用的程序集“”,因为它对不在当前目标框架“”具有依赖关系。请删除对不在目标框架中的程序集的引用,或考虑重新确定项目的目标。 Kevin
    The diffrence between Cast() and OfType() Kevin
    “System.Collections.Generic.IEnumerable<decimal>”不包含“ToArray”的定义,并且找不到可接受类型为“System.Collections.Generic.IEnumerable<decimal>”的第一个参数的扩展方法“ToArray” Kevin
    partial关键字的含义和使用 Kevin
  • 原文地址:https://www.cnblogs.com/KuroNJQ/p/11247264.html
Copyright © 2011-2022 走看看