标签:spring mvc拦截器 handlerinterceptor prehandle posthandle aftercompletion
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Spring MVC</span><span style="font-family: 宋体; background-color: rgb(255, 255, 255);">也可以使用拦截器对请求进行拦截处理,用户 可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现</span><span style="font-family: Arial, Helvetica, sans-serif; color: rgb(255, 0, 0); background-color: rgb(255, 255, 255);">HandlerInterceptor</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">接口</span>
① preHandle():这个方法在业务处理器处理请求之前被调用,在该方法中对用户请求 request 进行处理。如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;如果程序员决定不需要再调用其他的组件去处理请求,则返回false。
② postHandle():这个方法在业务处理器处理完请求后,但是DispatcherServlet 向客户端返回响应前被调用,在该方法中对用户请求request进行处理。
③ afterCompletion():这个方法在 DispatcherServlet 完全处理完请求后被调用,可以在该方法中进行一些资源清理的操作。
InterceptorController.java
package com.ibigsea.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class InterceptorController { @RequestMapping("/interceptor") public String interceptor(){ System.out.println("controller interceptor"); return "helloworld"; } }
OneInterceptor.java
package com.ibigsea.springmvc.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class OneInterceptor implements HandlerInterceptor { /** * 该方法在执行目标方法之前被调用. * * 若返回值为 true, 则继续调用后续的拦截器和目标方法. * 若返回值为 false, 则不会再调用后续的拦截器和目标方法. * 并执行 当前拦截器 前面执行过 preHandle 拦截器的 afterCompletion()方法 * * 可以 做权限,日志,事务等操作. */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("one interceptor preHandle"); return true; } /** * 执行目标方法之后, 在渲染视图之前, * 并且 preHandle 返回为true ,调用该方法 * * 可以对请求域中的属性或视图做出修改. * 可以 添加一些公共的信息 比如当前时间 之类 */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("one interceptor postHandle"); } /** * 渲染视图之后 被调用该方法,可以做释放资源等操作 */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("one interceptor afterCompletion"); } }
TwoInterceptor.java
package com.ibigsea.springmvc.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class TwoInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("two interceptor preHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("two interceptor preHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("two interceptor afterCompletion"); } }
Spring-mvc.xml:
<mvc:interceptors> <!-- 配置自定义的拦截器 --> <bean id="oneInterceptor" class="com.ibigsea.springmvc.interceptor.OneInterceptor"/> <bean id="twoInterceptor" class="com.ibigsea.springmvc.interceptor.TwoInterceptor"/> </mvc:interceptors>
运行结果:
one interceptor preHandle two interceptor preHandle controller interceptor two interceptor preHandle one interceptor postHandle two interceptor afterCompletion one interceptor afterCompletion one interceptor preHandle two interceptor preHandle
标签:spring mvc拦截器 handlerinterceptor prehandle posthandle aftercompletion
原文地址:http://blog.csdn.net/a67474506/article/details/46390741