Spring MVC 拦截器(转)
程序员文章站
2022-07-15 08:39:55
...
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 }
上一篇: c++ 动态数组类的创建和简单运用
推荐阅读
-
关于Spring MVC在Controller层中注入request的坑详解
-
干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结
-
基于Maven 的 Spring MVC
-
三、解决Spring MVC拦截器导致静态资源访问失败(基于java注解配置)
-
详解Spring mvc ant path的使用方法
-
Spring MVC之DispatcherServlet详解_动力节点Java学院整理
-
Spring MVC之DispatcherServlet_动力节点Java学院整理
-
Spring MVC五大核心组件和配置
-
Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
-
Spring MVC源码(一) ----- 启动过程与组件初始化