SpringMVC拦截器的配置

SpringMVC拦截器的配置

首先是XML文件的配置

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>  //拦截所有的请求
<!--            <mvc:exclude-mapping path="/login.jsp"/>-->  //不拦截的请求
            <bean class="com.baosight.iplat4j.dr.bd.interceptor.LoginInterceptor"/>  //拦截器的类名路径
        </mvc:interceptor>
    </mvc:interceptors>

特别注意XML的配置部分xsi:schemaLocation需要加上

       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       //需要4.3,不然会出现mvc:exclude-mapping报错

拦截器代码如下

public class LoginInterceptor implements HandlerInterceptor {

//以下 的三个方法,分别在请求前,控制器执行后,请求结束,三个位置执行。返回值为true时才会继续向下执行

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //写逻辑 注意加返回值

    }
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
      //写逻辑 注意加返回值
    }


    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {
   //写逻辑 注意加返回值
    }

}

猜你喜欢

转载自blog.csdn.net/qq_40844662/article/details/102721244