拦截器已在上一节介绍,下面直接示例演示。
一、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<!--配置spring xml的寻找路径-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
二、spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.lzj.springmvc"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:interceptors>
<!-- <bean class="com.lzj.springmvc.interceptors.FirstInterceptor"></bean>
-->
<mvc:interceptor>
<!--表示拦截根目录下的所有请求-->
<mvc:mapping path="/*"/>
<bean class="com.lzj.springmvc.interceptors.FirstInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
三、创建拦截器
package com.lzj.springmvc.interceptors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class FirstInterceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("FirstInterceptor -> afterCompletion");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("FirstInterceptor -> postHandle");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("FirstInterceptor -> preHandle");
return true;
}
}
四、创建一个index.jsp请求,其内容为:
<a href="helloworld">hello world</a>
五、创建控制器
用于截获index.jsp中的hello world请求
@Controller
@RequestMapping("/springMVC")
public class TestSpringMVC {
@RequestMapping("/testRequestMapping")
public String testRequestMapping(){
System.out.println("testRequestMapping");
return "success";
}
}
当点击index.jsp中的请求连接时,拦截器拦截请求,执行preHandle方法,然后控制器截获请求,执行控制器testRequestMapping方法,当控制器执行结束后并返回时,拦截器拦截请求,执行postHandle方法,最后释放资源,执行afterCompletion。输出结果如下:
preHandle
hello world
postHandle
afterCompletion
拦截器配置方法:spring中拦截器除了上面的配置方法
<!-- <bean class="com.lzj.springmvc.interceptors.FirstInterceptor"></bean>
-->
<mvc:interceptor>
<!--表示拦截根目录下的所有请求-->
<mvc:mapping path="/*"/>
<bean class="com.lzj.springmvc.interceptors.FirstInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
还可以按如下配置:
<mvc:interceptors>
<!-- <bean class="com.lzj.springmvc.interceptors.FirstInterceptor"></bean>
-->
<mvc:interceptor>
<!--表示根目录下的所有请求都不拦截-->
<mvc:exclude-mapping path="/*"/>
<bean class="com.lzj.springmvc.interceptors.FirstInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
拦截器执行顺序:如果有多个拦截器,那么多个拦截器执行的顺序是什么?在上面代码的基础上创建一个SecondInterceptor
package com.lzj.springmvc.interceptors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class SecondInterceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("SecondInterceptor -> afterCompletion");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("SecondInterceptor -> postHandle");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("SecondInterceptor -> preHandle");
return true;
}
}
spring的配置文件为
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="com.lzj.springmvc.interceptors.FirstInterceptor"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="com.lzj.springmvc.interceptors.SecondInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
当执行index.jsp中hello world请求时,console中打印出:
FirstInterceptor -> preHandle
SecondInterceptor -> preHandle
hello world
SecondInterceptor -> postHandle
FirstInterceptor -> postHandle
SecondInterceptor -> afterCompletion
FirstInterceptor -> afterCompletion
但是如果当FirstInterceptor 拦截器中preHandle返回false时,
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("FirstInterceptor -> preHandle");
return false;
}
此时console中只输出:
FirstInterceptor -> preHandle
此时过滤在第一个过滤器的preHandle方法就已经返回了,不会再执行第一个过滤器的postHandle和afterCompletion方法,也不会再执行后面的过滤器。
如果第FirstInterceptor 种的preHandle返回true,SecondInterceptor 中的preHandle返回false的话,执行index.jsp中的请求,console中输出:
FirstInterceptor -> preHandle
SecondInterceptor -> preHandle
FirstInterceptor -> afterCompletion