欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring MVC拦截器详解 博客分类: spring SpringMVC拦截器 

程序员文章站 2024-03-16 19:28:46
...

 

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 [com.gary.test.controller.DemoController@1ed1dbe] 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
相关标签: SpringMVC 拦截器