Spring MVC 拦截器(转)
程序员文章站
2022-07-15 08:29:09
...
Controller层的拦截器继承于HandlerInterceptorAdapter
HandlerInterceptorAdapter.java
HandlerInterceptorAdapter.java
1 public abstract class HandlerInterceptorAdapter implements HandlerInterceptor {
2
3 /**
4 * This implementation always returns <code>true</code>.
5 */
6 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
7 throws Exception {
8 return true;
9 }
10
11 /**
12 * This implementation is empty.
13 */
14 public void postHandle(
15 HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
16 throws Exception {
17 }
18
19 /**
20 * This implementation is empty.
21 */
22 public void afterCompletion(
23 HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
24 throws Exception {
25 }
26
27 }
Spring拦截器通过重写这三个方法实现Controller的拦截。2
3 /**
4 * This implementation always returns <code>true</code>.
5 */
6 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
7 throws Exception {
8 return true;
9 }
10
11 /**
12 * This implementation is empty.
13 */
14 public void postHandle(
15 HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
16 throws Exception {
17 }
18
19 /**
20 * This implementation is empty.
21 */
22 public void afterCompletion(
23 HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
24 throws Exception {
25 }
26
27 }
配置拦截器
xml
xml
1 <bean id="handlerMapping"
2 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
3 <property name="interceptors">
4 <list>
5 <ref bean="controllerInterceptor" />
6 </list>
7 </property>
8 <property name="mappings">
9 <props>
10 <prop key="/hao/hello.do">helloWorldController</prop>
11 </props>
12 </property>
13 </bean>
14
15 <bean id="controllerInterceptor" class="com.web.spring.mvc.interceptor.ControllerInterceptor"/>
16
ControllerInterceptor.java
2 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
3 <property name="interceptors">
4 <list>
5 <ref bean="controllerInterceptor" />
6 </list>
7 </property>
8 <property name="mappings">
9 <props>
10 <prop key="/hao/hello.do">helloWorldController</prop>
11 </props>
12 </property>
13 </bean>
14
15 <bean id="controllerInterceptor" class="com.web.spring.mvc.interceptor.ControllerInterceptor"/>
16
1 public class ControllerInterceptor extends HandlerInterceptorAdapter {
2
3 /**
4 * 在Controller方法前进行拦截
5 */
6 public boolean preHandle(HttpServletRequest request,
7 HttpServletResponse response, Object handler) throws Exception {
8 System.out.println("ControllerInterceptor.preHandle()");
9 return true;
10 }
11
12 /**
13 * This implementation is empty.
14 */
15 public void postHandle(HttpServletRequest request,
16 HttpServletResponse response, Object handler,
17 ModelAndView modelAndView) throws Exception {
18 System.out.println("ControllerInterceptor.postHandle()");
19 }
20
21 /**
22 * 在Controller方法后进行拦截
23 */
24 public void afterCompletion(HttpServletRequest request,
25 HttpServletResponse response, Object handler, Exception ex)
26 throws Exception {
27 System.out.println("ControllerInterceptor.afterCompletion()");
28 }
29 }
2
3 /**
4 * 在Controller方法前进行拦截
5 */
6 public boolean preHandle(HttpServletRequest request,
7 HttpServletResponse response, Object handler) throws Exception {
8 System.out.println("ControllerInterceptor.preHandle()");
9 return true;
10 }
11
12 /**
13 * This implementation is empty.
14 */
15 public void postHandle(HttpServletRequest request,
16 HttpServletResponse response, Object handler,
17 ModelAndView modelAndView) throws Exception {
18 System.out.println("ControllerInterceptor.postHandle()");
19 }
20
21 /**
22 * 在Controller方法后进行拦截
23 */
24 public void afterCompletion(HttpServletRequest request,
25 HttpServletResponse response, Object handler, Exception ex)
26 throws Exception {
27 System.out.println("ControllerInterceptor.afterCompletion()");
28 }
29 }
说明:
发起请求,进入拦截器链,运行所有拦截器的preHandle方法,
1.当preHandle方法返回false时,从当前拦截器往回执行所有拦截器的afterCompletion方法,再退出拦截器链。
2.当preHandle方法全为true时,执行下一个拦截器,直到所有拦截器执行完。再运行被拦截的Controller。然后进入拦截器链,运行所有拦截器的postHandle方法,完后从最后一个拦截器往回执行所有拦截器的afterCompletion方法.
当有拦截器抛出异常时,会从当前拦截器往回执行所有拦截器的afterCompletion方法
Controller
发起请求,进入拦截器链,运行所有拦截器的preHandle方法,
1.当preHandle方法返回false时,从当前拦截器往回执行所有拦截器的afterCompletion方法,再退出拦截器链。
2.当preHandle方法全为true时,执行下一个拦截器,直到所有拦截器执行完。再运行被拦截的Controller。然后进入拦截器链,运行所有拦截器的postHandle方法,完后从最后一个拦截器往回执行所有拦截器的afterCompletion方法.
当有拦截器抛出异常时,会从当前拦截器往回执行所有拦截器的afterCompletion方法
1
2 @Controller
3 @RequestMapping("/hao")
5 public class HelloWorldController {
6
7 @RequestMapping(value = "/hello")
8 public String hello(HttpServletRequest request, HttpServletResponse response) {
9 System.out.println("hello");
15 return "helloWorld";
16 }
17 }
2 @Controller
3 @RequestMapping("/hao")
5 public class HelloWorldController {
6
7 @RequestMapping(value = "/hello")
8 public String hello(HttpServletRequest request, HttpServletResponse response) {
9 System.out.println("hello");
15 return "helloWorld";
16 }
17 }
上一篇: 关于快递
下一篇: JAVA知识点全总结——(五)网络
推荐阅读
-
spring mvc配置bootstrap教程
-
MVC模式和Spring MVC初识
-
Spring入门(十四):Spring MVC控制器的2种测试方法
-
Spring MVC的文件下载实例详解
-
JSP开发中在spring mvc项目中实现登录账号单浏览器登录
-
ModelAndViewContainer、ModelMap、Model详细介绍【享学Spring MVC】
-
Spring Mvc下实现以文件流方式下载文件的方法示例
-
使用spring mvc+localResizeIMG实现HTML5端图片压缩上传的功能
-
使用spring拦截器手写权限认证框架
-
Spring MVC+mybatis实现注册登录功能