1. 拦截器是Spring中的一个概念。

1.XML配置方式

  1. <mvc:interceptors>
  2. <mvc:interceptor>
  3. <mvc:mapping path="/**"/>
  4. <mvc:exclude-mapping path="/testRequestEntity"/>
  5. <!-- <bean id="myInterceptor" class="xxx"></bean>-->
  6. <ref bean="firstInterceptor"></ref>
  7. </mvc:interceptor>
  8. </mvc:interceptors>

2.拦截器编写

  1. //如果通过xml配置也可以
  2. @Component
  3. public class FirstInterceptor implements HandlerInterceptor {
  4. @Override
  5. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  6. System.err.println("FirstInterceptor的preHandler");
  7. return false;
  8. }
  9. @Override
  10. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  11. System.err.println("First-Posthandle");
  12. }
  13. @Override
  14. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  15. System.err.println("First-afterCompletion");
  16. }
  17. }

3.执行顺序

image.png
image.png