拦截只针对Handler
1.继承HandlerInterceptor接口
@Component
public class MyIntertecptor implements HandlerInterceptor {
/**
* 渲染之后,释放资源等
*/
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("afterCompletion");
}
/**
* Handler之后,未渲染
*/
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("intertecptor postHandle");
}
/**
* 请求到达Handler之前
*/
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("intertecptor preHandle");
return true;
}
}
2.加载拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/getImg"/>
<bean class="com.neu.intertecptor.MyIntertecptor"></bean>
</mvc:interceptor>
<ref bean="myIntertecptor2"/>
....
</mvc:interceptors>
拦截器链:MyIntertecptor,myIntertecptor2….
不设定mapping path默认针对所有请求
拦截器的执行顺序和过滤器相同