标签:c style class blog code java
Spring为我们提供了:org.springframework.web.servlet.HandlerInterceptor接口,
org.springframework.web.servlet.handler.HandlerInterceptorAdapter适配器,
实现这个接口或继承此类,能够很方便的实现自己的拦截器。
有下面三个方法:
Action之前运行
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e)
在preHandle中,能够进行编码、安全控制等处理;
在postHandle中,有机会改动ModelAndView;
在afterCompletion中,能够依据ex是否为null推断是否发生了异常,进行日志记录。
參数中的Object
handler是下一个拦截器。
怎样使用拦截器
自己定义一个拦截器,要实现HandlerInterceptor接口:
public class MyInterceptor implements HandlerInterceptor {...}
一、拦截全部URL
<mvc:interceptors> <bean class="com.itmyhome.MyInterceptor" /> </mvc:interceptors>
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/login" /> <bean class="com.itmyhome.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <bean class="com.itmyhome.MyInterceptor"></bean> </list> </property> </bean>
AnnotationMethodHandlerAdapter 这两个bean,所以就没有机会再给它注入interceptors属性,就无法指定拦截器。
当然我们能够通过人工配置上面的两个Bean,不使用 <mvc:annotation-driven />,
就能够给interceptors属性注入拦截器了。
拦截器MyInterceptor类
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class MyInterceptor implements HandlerInterceptor { public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) throws Exception { System.out.println("******afterCompletion******"); } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView arg3) throws Exception { System.out.println("******postHandle******"); } /** * 假设返回false 从当前拦截器往回运行全部拦截器的afterCompletion方法,再退回拦截器链 假设返回true * 运行下一个拦截器,直到全部拦截器都运行完成 再运行被拦截的Controller * 然后进入拦截器链从最后一个拦截器往回运行全部拦截器的postHandle方法 * 接着依然是从最后一个拦截器往回运行全部拦截器的afterCompletion方法 */ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("******preHandle******"); return true; } }
<!-- 拦截所以URL <mvc:interceptors> <bean class="com.itmyhome.MyInterceptor" /> </mvc:interceptors> --> <!-- 拦截匹配URL --> <mvc:interceptors > <mvc:interceptor> <mvc:mapping path="/login" /> <bean class="com.itmyhome.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!-- HandlerMappint上的拦截器 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <bean class="com.itmyhome.MyInterceptor"></bean> </list> </property> </bean>--> <!-- 默认扫描的包路径--> <context:component-scan base-package="com.itmyhome" /> <!-- 加入?注解驱动 --> <mvc:annotation-driven /> <!-- 假设使用HandlerMappint拦截器则凝视以上注冊驱动的方法,使用下面人工配置bean --> <!-- <bean class="com.itmyhome.Login"></bean>--> <!-- 处理器 --> <bean name="/login" class="com.itmyhome.Login"></bean> <!-- HandlerMapping <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>--> <!-- HandlerAdapter <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>--> <!-- 定义跳转的文件的前后缀 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <!-- 默认放在WebRoot下 --> <property name="suffix" value=".jsp" /> </bean>
查看后台会运行拦截器代码
项目源代码下载:http://download.csdn.net/detail/itmyhome/7372023
springMVC3学习(七)--Interceptor拦截器,布布扣,bubuko.com
springMVC3学习(七)--Interceptor拦截器
标签:c style class blog code java
原文地址:http://www.cnblogs.com/blfshiye/p/3762465.html