1.拦截器作用
拦截控制方法的执行
2.实现
package com.cj.rest.interceptors; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class FirstInterceptors implements HandlerInterceptor { // 控制器执行之前 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("FirstInterceptors--preHandle"); return true; } // 控制器执行之后 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("FirstInterceptors--postHandle"); } // 视图渲染完成之后 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("FirstInterceptors--afterCompletion"); } }
xml
<mvc:interceptors> - <bean class="com.cj.rest.interceptors.FirstInterceptors"></bean>--> - <ref bean="firstInterceptors"/>--> <mvc:interceptor> <mvc:mapping path="/**"/> //拦截所有 <mvc:exclude-mapping path="/"/> <ref bean="firstInterceptors"/> </mvc:interceptor> </mvc:interceptors>
标签:HttpServletRequest,拦截器,--,HttpServletResponse,FirstInterceptors,import,public From: https://www.cnblogs.com/cciscc/p/16820970.html