zoukankan      html  css  js  c++  java
  • springMVC拦截器的使用

    1、新建一个config包和一个配置拦截类MyInterceptor,并继承接口HandlerInterceptor.

    快捷键(Ctrl + o)实现该接口的三个方法:

    public class MyInterceptor implements HandlerInterceptor {
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            System.out.println("================处理前=============");
            return false;
        }
        //拦截日志
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            System.out.println("=================处理后==============");
        }
        //拦截日志
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            System.out.println("=================清理==============");
        }
    }

    其中:

    ① preHandle方法在前端URL请求前处理,默认返回false表示禁止所有通过url访问的请求,true表示允许。

    ② postHandle方法在前端URL请求中处理,一般默认使用日志。afterCompletion类似。

    2、在spring类路径配置文件applicationContext.xml中追加配置类的信息

    <mvc:interceptors>
            <mvc:interceptor>
                <!--过滤哪个请求-->
                <mvc:mapping path="/**"/>
                <!--由谁来过滤-->
                <bean class="com.meng.config.MyInterceptor"/>
            </mvc:interceptor>
        </mvc:interceptors>
  • 相关阅读:
    Git 数据是怎么存储的
    技术管理规划-路径跟资源
    技术管理规划-如何规划团队的架构
    技术管理规划-如何设定团队的目标
    技术管理规划-设定团队的职能
    springboot实践1
    spring的事件机制实战
    Apollo的基本概念和集成实战
    spring的事件
    ELK的简单安装使用
  • 原文地址:https://www.cnblogs.com/Meng2113/p/13503575.html
Copyright © 2011-2022 走看看