SpringMVC拦截器
作者:互联网
一、实现接口
接口HandlerInterceptor 有:
preHandle():在目标方法运行之前调用;return true放行,return false不放行
postHandle():在目标方法运行之后调用
afterCompletion():在请求整个完成之后;来到目标页面之后调用
拦截器MyFirstInterceptor
public class MyFirstInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyFirstInterceptor preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MyFirstInterceptor postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("MyFirstInterceptor afterCompletion");
}
}
拦截器MySecondInterceptor
public class MySecondInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MySecondInterceptor preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MySecondInterceptor postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("MySecondInterceptor afterCompletion");
}
}
二、配置拦截器
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描所有组件-->
<context:component-scan base-package="com.example"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<mvc:interceptors>
<!--配置某个拦截器,默认是拦截所有请求的-->
<bean class="com.example.interceptor.MyFirstInterceptor"/>
<!--配置某个拦截器更详细的信息-->
<mvc:interceptor>
<mvc:mapping path="/handle01"/>
<bean class="com.example.interceptor.MySecondInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
三、测试
HelloController
@Controller
public class HelloController {
@RequestMapping("/handle01")
public String handle01(Map<String, Object> map){
System.out.println("handle01");
return "success.jsp";
}
}
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
System.out.println("success.jsp");
%>
<h4>success</h4>
</body>
</html>
进行访问:
http://localhost:8080/handle01
结果:
四、拦截流程
1.正常拦截流程:
preHandle、目标方法、postHandle、页面、拦截器afterCompletion
拦截器的preHandle:是按照顺序执行;拦截器的postandle:是按照逆序执行;拦截器的afterCompletion:是按照逆序执行
2.其他拦截流程:
只要preHandler不放行就没有以后的流程;只要放行,afterCompletion都会执行
栗:MyFirstInterceptor放行,MySecondInterceptor不放行
标签:Exception,拦截器,preHandle,SpringMVC,System,public,afterCompletion 来源: https://blog.csdn.net/Luck_ZZ/article/details/100676753