SpringMVC拦截器interceptor
1.概念
Spring MVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理进行预处理
和后处理
。
将拦截器按一定的顺序联结成一条链,这条链称为拦截器链(Interceptor Chain)
。在访问被拦截的方
法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。拦截器也是AOP思想
的具体实现。
2.拦截器和过滤器的区别
区别 | 过滤器Filter | 拦截器Interceptor |
---|---|---|
使用范围 | 是 servlet 规范中的一部分,任何Java Web 工程都可以使用 | 是 SpringMVC 框架自己的,只有使用了SpringMVC 框架的工程才能用 |
拦截范围 | 在 url-pattern 中配置了/* 之后,可以对所有要访问的资源拦截 | 在<mvc:mapping path=“”/>中配置了/** 之后,也可以多所有资源进行拦截,但是可以通过<mvc:exclude-mapping path=“”/>标签排除不需要拦截的资源 |
3.拦截器快速入门
自定义拦截器很简单,只有如下三步:
① 创建拦截器类实现HandlerInterceptor接口
② 配置拦截器
③ 测试拦截器的拦截效果
1.创建拦截器类实现HandlerInterceptor接口
- 创建一个检测脏话的拦截器ShitWordInterceptor,实现HandlerInterceptor接口
//脏话拦截器,要实现HandlerInterceptor接口
public class ShitWordInterceptor implements HandlerInterceptor {
//在目标方法执行前执行
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("interceptor1...preHandle...");
String word = request.getParameter("word");
if ("sb".equals(word)) { //如果检测到脏话,则转发到error.jsp
request.getRequestDispatcher("/error.jsp").forward(request, response);
return false;
} else {
return true; //返回true方行,返回false不放行
}
}
//在执行方法之后,返回ModelAndView之前执行
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("interceptor1...postHandle...");
}
//在流程执行完毕后执行
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) throws Exception {
System.out.println("interceptor1...afterCompletion...");
}
}
2.创建完自定义拦截器后,要在spring-mvc.xml中配置拦截器
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--注解驱动-->
<mvc:annotation-driven/>
<!--自动扫包-->
<context:component-scan base-package="com.itspring.controller"/>
<!--配置内部资源视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置拦截器-->
<mvc:interceptors>
<!--脏话拦截器-->
<mvc:interceptor>
<!--对所有请求拦截-->
<mvc:mapping path="/**"/>
<!--自定义拦截器类-->
<bean class="com.itspring.interceptor.ShitWordInterceptor"/>
</mvc:interceptor>
<!--其他拦截器-->
<!--<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class=""/>
</mvc:interceptor>-->
</mvc:interceptors>
</beans>
3.编写controller
@Controller
public class WordController {
@RequestMapping("/index")
public ModelAndView index(String word) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("word", word);
modelAndView.setViewName("index");
return modelAndView;
}
}
4.测试