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向客户端返回响应前被调用 */ @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>
控制台输出:
====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=====
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/luckey_zh/article/details/46883691