SpringMVC拦截器
程序员文章站
2022-06-10 20:05:19
...
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 }
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的拦截。
配置拦截器
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 }
下一篇: Spring AOP网上例子整理(一)