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>
  • 相关阅读:
    多语言资源文件制作工具
    window service 插件服务插件开发
    .net 中读取自定义Config文件
    Asp.net 主题中CSS文件的缓存问题
    Asp .net 4.0 中ViewStatus 使用
    Linq通用分页数据查询方法
    EF中查询出现死锁的处理
    Windows Live Writer 分享到插件
    Windows Resx资源文件编辑工具
    插件式服务架构
  • 原文地址:https://www.cnblogs.com/Meng2113/p/13503575.html
Copyright © 2011-2022 走看看