码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot-web综合

时间:2019-01-03 16:46:48      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:mode   注册   att   handle   pattern   pos   oid   creat   gis   

  1. 自定义Filter
    public class MyFilter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("filter init");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            String uri = request.getRequestURI();
            System.out.println("filter uri : " + uri);
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            System.out.println("filter destroy");
        }
    }
    

     

  2. 自定义Listener
    /**
     * 描述:
     * 1.ServletContextListener -- 监听servletContext对象的创建以及销毁
     * 2.HttpSessionListener  -- 监听session对象的创建以及销毁
     * 3.ServletRequestListener -- 监听request对象的创建以及销毁
     * 4.ServletContextAttributeListener  -- 监听servletContext对象中属性的改变
     * 5.HttpSessionAttributeListener  --监听session对象中属性的改变
     * 6.ServletRequestAttributeListener  --监听request对象中属性的改变
     * @outhor ios
     * @create 2019-01-03 3:00 PM
     */
    public class MyListener implements ServletRequestListener {
    
        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            System.out.println("---------------------------->请求销毁");
        }
    
        @Override
        public void requestInitialized(ServletRequestEvent sre) {
            System.out.println("---------------------------->请求创建");
        }
    }
    

     

  3. 自定义interceptor
    public class MyInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("拦截器 -- 请求处理之前");
            return false;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            System.out.println("拦截器 -- 请求处理之后,视图渲染之前");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            System.out.println("拦截器 -- 请求处理之后,视图渲染之后");
        }
    }
    

     

  4. 注册自定义的Filter,Listener,Intercetor
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Bean
        public FilterRegistrationBean filterRegistrationBean(){
            FilterRegistrationBean bean = new FilterRegistrationBean();
            bean.setFilter(new MyFilter());
            bean.setName("MyFilter");
            bean.addUrlPatterns("/*");
            return bean;
        }
    
        @Bean
        public ServletListenerRegistrationBean listenerRegistrationBean(){
            ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
            bean.setListener(new MyListener());
            return bean;
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
        }
    }
    

     

SpringBoot-web综合

标签:mode   注册   att   handle   pattern   pos   oid   creat   gis   

原文地址:https://www.cnblogs.com/king-peng/p/10214535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!