Spring MVC也能够使用拦截器对请求进行拦截处理,用户能够自己定义拦截器来实现特定的功能,自己定义的拦截器必须实现HandlerInterceptor接口。
直接看下样例:
package com.ztz.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 MyInterceptor implements HandlerInterceptor{ /** * 业务处理器处理之前被调用。被拦截返回false,反之能正常到Controller层 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("====preHandle===="); //假设返回false将不会到Controller层 return true; } /** * 在业务处理完毕请求后。在DispatcherServlet向client返回响应前被调用 */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("======postHandle====="); } /** * 在DispatcherServlet全然处理完请求后被调用(能够在该方法进行一些资源的清理操作) */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("=====afterCompletion======="); } }
package com.ztz.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/interceptor") public class MyInterceptorController { @RequestMapping("/inter1") private String interceptor1(){ System.out.println("=====inter1====="); return "index"; } @RequestMapping("/inter2") private String interceptor2(){ System.out.println("======inter2====="); return "index"; } }
<!--默认的mvc注解映射的支持 --> <mvc:annotation-driven /> <!-- 处理对静态资源的请求 --> <mvc:resources location="/static/" mapping="/static/**" /> <!-- 扫描注解 --> <context:component-scan base-package="com.ztz.springmvc.controller" /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> <!-- 拦截器配置 --> <mvc:interceptors> <!-- 多个拦截器,顺序运行 --> <mvc:interceptor> <!-- 须要拦截的url --> <mvc:mapping path="/interceptor/*" /> <!-- 拦截器实现类 --> <bean class="com.ztz.springmvc.interceptor.MyInterceptor" /> </mvc:interceptor> </mvc:interceptors>
浏览器请求:http://127.0.0.1:8080/springmvc/interceptor/inter1
控制台输出:
====preHandle====
=====inter1=====
======postHandle=====
=====afterCompletion=======
我们看到<mvc:mapping path="/interceptor/*" />拦截的是/interceptor/路径下全部的请求,那么如今我不想拦截 /interceptor/inter2这个请求怎么办。这个非常好实现的。仅仅须要加一个配置。
<!-- 拦截器配置 --> <mvc:interceptors> <!-- 多个拦截器,顺序运行 --> <mvc:interceptor> <!-- 须要拦截的url --> <mvc:mapping path="/interceptor/*" /> <!-- 不拦截该请求 --> <mvc:exclude-mapping path="/interceptor/inter2"/> <!-- 拦截器实现类 --> <bean class="com.ztz.springmvc.interceptor.MyInterceptor" /> </mvc:interceptor> </mvc:interceptors>浏览器请求:http://127.0.0.1:8080/springmvc/interceptor/inter2
控制台输出:
======inter2=====
PS:本demo地址:http://download.csdn.net/detail/zwz568017880/9043089