Spring MVC拦截器详解
程序员文章站
2022-05-28 17:35:05
...
package com.gary.util.spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**
* Spring MVC拦截器
* @author gary
*
*/
public class CommonInterceptor extends HandlerInterceptorAdapter implements InitializingBean{
static Log log = LogFactory.getLog(CommonInterceptor.class);
/**
* 在系统启动时执行
*/
public void afterPropertiesSet() throws Exception {
log.debug("=======初始化CommonInterceptor拦截器=========");
}
/**
* 在Controller方法前进行拦截
* 如果返回false
* 从当前拦截器往回执行所有拦截器的afterCompletion方法,再退出拦截器链.
* 如果返回true
* 执行下一个拦截器,直到所有拦截器都执行完毕.
* 再运行被拦截的Controller.
* 然后进入拦截器链,从最后一个拦截器往回运行所有拦截器的postHandle方法.
* 接着依旧是从最后一个拦截器往回执行所有拦截器的afterCompletion方法.
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
log.debug("=====preHandle====");
//业务逻辑
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
log.debug("==========postHandle=========");
if(modelAndView != null){
String viewName = modelAndView.getViewName();
log.debug("view name : " + viewName);
}else{
log.debug("view is null");
}
}
/**
* 在Controller方法后进行拦截
* 当有拦截器抛出异常时,会从当前拦截器往回执行所有拦截器的afterCompletion方法
*/
@Override
public void afterCompletion(HttpServletRequest httpservletrequest,
HttpServletResponse httpservletresponse, Object obj,
Exception exception) throws Exception {
log.debug("=====afterCompletion====");
}
}
部分配置
<property name="interceptors"> <list> <bean class="com.gary.util.spring.CommonInterceptor"/> <bean class="com.gary.util.spring.CommonInterceptor2"/> </list> </property>
实际运行结果(控制台DEBUG输出)
DispatcherServlet with name 'springmvc' processing GET request for [/demo/Demo/test.html] Matching patterns for request [/Demo/test.html] are [/Demo/*] URI Template variables for request [/Demo/test.html] are {} Mapping [/Demo/test.html] to HandlerExecutionChain with handler [[email protected]] and 1 interceptor Returning handler method name 'test' for lookup path: /Demo/test.html Last-Modified value for [/demo/Demo/test.html] is: -1 =====preHandle==== =====preHandle2==== Returning handler method name 'test' for lookup path: /Demo/test.html 进入到demo的test方法 ==========postHandle2========= view name : /Demo/test ==========postHandle========= view name : /Demo/test Invoking afterPropertiesSet() on bean with name '/Demo/test' Rendering view [org.springframework.web.servlet.view.JstlView: name '/Demo/test'; URL [/Demo/test.jsp]] in DispatcherServlet with name 'springmvc' Forwarding to resource [/Demo/test.jsp] in InternalResourceView '/Demo/test' =====afterCompletion2==== =====afterCompletion==== Successfully completed request
推荐阅读
-
详解spring中使用Elasticsearch的代码实现
-
详解使用Spring Boot开发Restful程序
-
详解spring整合shiro权限管理与数据库设计
-
详解spring中使用solr的代码实现
-
详解spring boot使用@Retryable来进行重处理
-
详解spring-boot集成elasticsearch及其简单应用
-
通过Spring AOP实现Spring MVC自定义注解验证器 博客分类: springspring-mvcAOP springspring-mvcAOP
-
Spring Boot实战教程之自动配置详解
-
详解Spring Security如何配置JSON登录
-
关于spring的两个拦截器的问题 SpringDAO配置管理HibernateAOP