• springMVC拦截器和过滤器总结


    拦截器: 用来对访问的url进行拦截处理

    用处: 权限验证,乱码设置等

    spring-mvc.xml文件中的配置:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
                  xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                            http://www.springframework.org/schema/tx
                            ">
        <!--编写拦截器-->
        <mvc:interceptors>
            <!--对特定的url拦截-->
            <mvc:interceptor>
                <mvc:mapping path="/test.do"/>
                <bean class="com.hbut.interceptor.TestInterceptor"/>
            </mvc:interceptor>
            <mvc:interceptor>
                <!--对特定的模块拦截第一级别拦截-->
                <mvc:mapping path="/test/×/"/>
                <bean class="com.hbut.interceptor.TestInterceptor1"/>
            </mvc:interceptor>
            <mvc:interceptor>
                <!--对特定的模块拦截-->
                <mvc:mapping path="/test/×"/>
                <bean class="com.hbut.interceptor.TestInterceptor2"/>
            </mvc:interceptor>

    </mvc:interceptors>

    对所有的url进行拦截

      <mvc:interceptors>
          <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
      </mvc:interceptors>

    java代码

    package com.hbut.interceptor;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * @Author XiJun.Gong
     * @DATE 2016/6/1.
     * aim:   com.hbut.interceptor
     */
    public class TestInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            
    //todo 在此处添加要操作code
    System.out.println(
    "preHandle"); return true; //todo 此处为false时,请求不会到达control层 } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); //todo 可以用来修改信息,跳转等 } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion"); //todo 最后执行 } }


    另一种拦截器:大同小异

    package com.hbut.interceptor;
    
    import org.springframework.ui.ModelMap;
    import org.springframework.web.context.request.WebRequest;
    import org.springframework.web.context.request.WebRequestInterceptor;
    
    /**
     * @Author XiJun.Gong
     * @DATE 2016/6/1.
     * aim:   com.hbut.interceptor
     */
    public class Test2Interceptor implements WebRequestInterceptor {
        @Override
        public void preHandle(WebRequest webRequest) throws Exception {
        }
    
        @Override
        public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
    
        }
    
        @Override
        public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {
    
        }
    }

    过滤器: 依赖于servlet容器,使用回调函数,过滤范围大

    拦截器: 依赖于框架容器 比如spring、mybatis ,灵活

    参考文章: 1. http://blog.sina.com.cn/s/blog_6a0cd5e501012bt9.html

                   2.  http://haohaoxuexi.iteye.com/blog/1750680

                

  • 相关阅读:
    JavaScript在Javascript中为String对象添加trim,ltrim,rtrim方法
    JavaScriptjs写的俄罗斯方块
    WinForm中“嵌入的资源”和“资源文件”数据的获取方式
    Facade模式(外观模式)
    windows服务安装程序中如何安装后自动启动
    水晶报表之主从多表数据源批量预览及打印开发设计
    IP地址分类简介
    水晶报表之各节的作用
    水晶报表开发之常用代码以及注意事项
    .Net中后台线程和前台线程的区别
  • 原文地址:https://www.cnblogs.com/gongxijun/p/5551542.html
Copyright © 2020-2023  润新知