1.首先在servlet.xml文件中添加配置属性
<!--拦截器配置,多个拦截器,顺序执行-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<mvc:exclude-mapping path="/*/*" />
<bean class="com.springmvc.common.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
注意: (1) mvc:mapping表示拦截什么类型的请求,可以设置多个拦截
(2) exclude-mapping表示什么类型的请求,不会拦截
(3) bean 中的文件为自定义的拦截器
2.自定义的拦截器, 具体的逻辑就因人而异了
package com.springmvc.common;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
public class MyInterceptor implements WebRequestInterceptor {
public void preHandle(WebRequest request) throws Exception {
System.out.println("preHandle");
}
public void postHandle(WebRequest request, ModelMap map) throws Exception {
System.out.println("postHandle");
}
public void afterCompletion(WebRequest request, Exception exception)
throws Exception {
// TODO Auto-generated method stub
System.out.println("afterCompletion");
}
}