HandlerInterceptor翻译过来就是spring拦截器,它在某些功能应用上特别有用:
1. 用户是否登陆以及用户权限管理 (见http://www.ideawu.net/ideablog/category4/article174.html)
2. 根据用户的选择来决定是用HTML还是用Excel来作为View (该应用后面会讲解)
3. blackboard building block的应用:在每一个controller之前都要生成context,在render view之后都要release context
HandlerInterceptor接口有几个重要的方法:
preHandleAction(ActionRequest request, ActionResponse response, Object handler):
该方法会在Controller的方法执行前会被调用,可以使用这个方法来中断或者继续执行链的处理,当返回true
时,处理执行链会继续,当返回false
时,则不会去执行Controller的方法。(验证用户是否登陆就是使用preHandleAction方法的最好例子)
afterActionCompletion(ActionRequest request, ActionResponse response, Object handler, Exception ex)
preHandleRender(RenderRequest request, RenderResponse response, Object handler)
postHandleRender(RenderRequest request, RenderResponse response, Object handler, ModelAndView modelAndView)
这3个方法会在在controller的方法执行之后,在DispatcherServlet类导向到view进行render之前依次执行。最有用的是使用postHandleRender方法,因为它有ModelAndView 传进来,那么我们就可以在render view之前往view中添加额外的model对象,或者对view的去处进行修改(例如下面的“用HTML还是用Excel来作为View ”例子就是对view进行了更改)
afterRenderCompletion(RenderRequest request, RenderResponse response, Object handler, Exception ex)
该方法会在render view完成后执行,也可以说在请求过程(request processing)完成之后执行。该方法可以用来清理资源(例如象blackboard building block release context)
总结一下HandlerInterceptor的流程:
- (
DispatcherServlet
maps a request to particular handler and assembles a handler execution chain consisting of the handler that is to be invoked and all of theHandlerInterceptor
instances that apply to the request.) -
preHandleAction(..)
is called; if the invocation of this method returnstrue
then this workflow continues - The target handler handles the action phase of the request (via
HandlerAdapter.handleAction(..)
) -
afterActionCompletion(..)
is called -
preHandleRender(..)
is called; if the invocation of this method returnstrue
then this workflow continues - The target handler handles the render phase of the request (via
HandlerAdapter.handleRender(..)
) -
postHandleRender(..)
is called -
afterRenderCompletion(..)
is called
我们自己写的拦截器可以不直接实现HandlerInterceptor,而是扩展实现了HandlerInterceptor接口的具体类HandlerInterceptorAdapter,这样的话我们不需要把上面5个方法都实现,而只需要override我们需要的方法就可以了!
下面举一个使用拦截器来实现“用HTML还是用Excel来作为View ”功能的例子(只列出关键代码):
search.html:
Search for:
<input type="text" name="query"><br>
Output in:
<input type="radio name="format" value="xls"/>Excel or
<input type="radio" name="format" value="html"/>Html<br>
<input type="submit"/>
SearchController.java:
protected ModelAndView handleRequestInternal(
HttpServletRequest req, HttpServletResponse response {
String query = RequestUtils.getRequiredStringParameter("query");
List shows; // retrieve shows matching the query
ModelAndView mav = new ModelAndView("listshows", "shows", shows);
return mav;
}
OutputFormatModificationInterceptor.java:
public class OutputFormatModificationInterceptor extends HandlerInterceptorAdapter { private String parameter; private String defaultSuffix = ""; private Properties suffices; // setters and getters omitted public void postHandler( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) throws Exception { String format = request.getParameter("format"); String suffix = suffices.getProperty(format); // modify view name if exists if (suffix != null) { mav.setViewName(mav.getViewName() + suffix); //修改view达到使用不同的output格式的目的 } else { mav.setViewName(mav.getViewName() + defaultSuffix); } }
configure xml file:
相关推荐
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2292476
1.25 Spring Boot 拦截器HandlerInterceptor 1.26 Spring Boot启动加载数据CommandLineRunner 1.27 Spring Boot环境变量读取和属性对象的绑定 1.28 Spring Boot使用自定义的properties 1.29 改变自动扫描的包 1.30 ...
主要介绍了springMVC拦截器HandlerInterceptor用法代码示例,具有一定借鉴价值,需要的朋友可以参考下
主要介绍了Spring拦截器HandlerInterceptor接口代码解析,具有一定借鉴价值,需要的朋友可以参考下
6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...
spring mvc 拦截器获取请求数据信息 解压之后放到项目中 直接运行就可以了 (将流多次运用)
常用拦截 拦截器HandlerInterceptor 拦截器MethodInterceptor 添加依赖 创建启动类 创建拦截器类 创建控制器 监控control请求耗时,提高性能
Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack ...19.2. 使用Spring JMS ...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
多数据源调整和修改,支持数据源的切换等功能
spring aop切面拦截指定类和方法实现流程日志跟踪 一般情况下,在不侵入业务代码的情况下,需要做流程日志跟踪是比较合理的 采用springaop切面思想
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
开源地址系统要求基于 spring-boot 开发(spring-boot1.x, spring-boot2.x 均可)需要使用 redis工作流程kk-anti-reptile 使用 对请求进行过滤,通过 spring-boot 的扩展点机制,实例化一个Spring HandlerInterceptor ...
HandlerInterceptor HandlerInterceptorAdapter HandlerInterceptorAdapter HandlerMapping HandlerMapping HashMapCachingAdvisorChainFactory Hessian1SkeletonInvoker Hessian2SkeletonInvoker ...
1.架构: spring boot + jpa + thyemleaf, mysql数据库 2.功能实现: 1)使用Logback进行日志记录; 2)使用HandlerInterceptor接口实现登陆认证 3)登陆验证码的生成
spring的web框架,详细的介绍有关框架的概述,DispatcherServlet、HandlerMapping、Controller、ModelAndView、ViewResolver、HandlerInterceptor的相关介绍
主要介绍了spring-boot 使用Aop通知打印控制器请求报文和返回报文,非常不错,具有参考借鉴价值,需要的朋友可以参考下
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................