Filter过滤器是由servlet创建,interceptor是由Spring容器创建,
http://blog.csdn.net/zqlsnow/article/details/52946826中有这两个的区别
1.SpringMVC单个拦截器的实现
1)实现HandlerInterceptor接口
public class HandlerInterceptor1 implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("=====处理请求之前======HandlerInterceptor1 preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("====处理请求后,渲染页面前=======HandlerInterceptor1 postHandle");
}
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("=====视图渲染结束了,请求处理完毕======HandlerInterceptor1 afterCompletion");
}
}
2)XML配置拦截器信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
</beans>
<mvc:interceptors>
<!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->
<!-- <bean class="com.bybo.aca.web.interceptor.Login"/> -->
<mvc:interceptor>
<!-- 进行拦截:/**表示拦截所有controller -->
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/index.html"/> // spring-mvc-3.2.xsd以上版本才支持此标签
<!-- 处理拦截的具体实现类 -->
<bean class="com.readbook.interceptor.HandlerInterceptor1" />
</mvc:interceptor>
</mvc:interceptors>
2. SpringMVC多个拦截器的实现
https://www.cnblogs.com/mao2080/p/6769680.html
<mvc:interceptors>
<!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->
<!-- <bean class="com.bybo.aca.web.interceptor.Login"/> -->
<mvc:interceptor>
<!-- 进行拦截:/**表示拦截所有controller -->
<mvc:mapping path="/**" />
<!-- 不进行拦截的路径 -->
<mvc:exclude-mapping path="/index.html"/>
<!-- 处理拦截的具体实现类 -->
<bean class="com.readbook.interceptor.HandlerInterceptor1" />
</mvc:interceptor>
<mvc:interceptor>
<!-- 进行拦截:/**表示拦截所有controller -->
<mvc:mapping path="/**" />
<!-- 不进行拦截的路径 -->
<mvc:exclude-mapping path="/index.html"/>
<!-- 处理拦截的具体实现类 -->
<bean class="com.readbook.interceptor.HandlerInterceptor2" />
</mvc:interceptor>
</mvc:interceptors>
3. SpringMVC拦截器在具体方法中进行拦截
http://blog.csdn.net/qq_33571718/article/details/53046355