转自师兄:@郑清
注意:拦截器是框架中的技术,拦截的是controller中执行的方法
springmvc配置文件:
-
<!-- 配置SpringMVC拦截器 -->
-
<mvc:interceptors>
-
<!-- 一个拦截器 -->
-
<mvc:interceptor>
-
<!-- 要拦截的配置 -->
-
<mvc:mapping path="/*" />
-
<!-- 设置不拦截的配置 -->
-
<mvc:exclude-mapping path="/login.do" />
-
<!-- 配置拦截器 -->
-
<bean class="com.zhengqing.controller.MyInterceptor" />
-
</mvc:interceptor>
-
</mvc:interceptors>
自定义类继承HandlerInterceptor:
-
public
class MyInterceptor implements HandlerInterceptor {
-
-
@Override
-
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object object) throws Exception {
-
System.out.println(
"===拦截===");
-
return
true;
//false为拦截,true为放行
-
}
-
-
@Override
-
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
-
System.out.println(
"===处理完请求之后===");
-
}
-
-
@Override
-
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
-
System.out.println(
"===DispatcherServlet完结之后调用===");
-
}
-
-
}