本文实现一个基于Spring4.0的Interceptor拦截Controller请求的demo。更多的详细介绍在后续完善。
本例中的BaseInterceptor类HandlerInterceptor,并实现两个自定义的接口用来分发get和post请求处理。子类通过继承BaseInterceptor类然后重载其中关心的方法来拦截处理不同的请求。你也可以直接实现HandlerInterceptor并在对应的方法中直接进行拦截请求处理。
BaseInterceptor.java代码如下:
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.landsem.update.base.interceptors.IGetInterceptor; import com.landsem.update.base.interceptors.IPostInterceptor; public abstract class BaseInterceptor implements HandlerInterceptor, IGetInterceptor, IPostInterceptor { @Override public final void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // do get if (arg0.getMethod().equalsIgnoreCase("get")) { afterGetHandlerCompletion(arg0,arg1,arg2,arg3); } // do post else if (arg0.getMethod().equalsIgnoreCase("post")) { afterPostHandlerCompletion(arg0,arg1,arg2,arg3); } } @Override public final void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // do get if (arg0.getMethod().equalsIgnoreCase("get")) { postGetHandle(arg0,arg1,arg2,arg3); } // do post else if (arg0.getMethod().equalsIgnoreCase("post")) { postPostHandle(arg0,arg1,arg2,arg3); } } @Override public final boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { // do get if (arg0.getMethod().equalsIgnoreCase("get")) { return preGetHandle(arg0, arg1, arg2); } // do post else if (arg0.getMethod().equalsIgnoreCase("post")) { return prePostHandle(arg0, arg1, arg2); } // default return true; } @Override public void afterGetHandlerCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) { // TODO Auto-generated method stub } @Override public void postGetHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean preGetHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { // TODO Auto-generated method stub return true; } @Override public void afterPostHandlerCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) { // TODO Auto-generated method stub } @Override public void postPostHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean prePostHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { // TODO Auto-generated method stub return true; } }
IGetInterceptor.java代码如下:
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; public interface IGetInterceptor { public void afterGetHandlerCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3); public void postGetHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception; public boolean preGetHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception; }
IPostInterceptor.java的代码如下:
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; public interface IPostInterceptor { public void afterPostHandlerCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3); public void postPostHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception; public boolean prePostHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception; }
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.servlet.ModelAndView; import com.landsem.update.base.interceptors.impl.BaseInterceptor; import com.landsem.update.cache.service.ICacheService; public class DemoInterceptor extends BaseInterceptor { protected static Logger logger = Logger.getLogger(Demo .class); @Override public void afterPostHandlerCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) { if (logger.isDebugEnabled()) { logger.debug("Demo afterCompletion"); } } @Override public void postPostHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { if (logger.isDebugEnabled()) { logger.debug("PublishUpdateInterceptor postPostHandle"); } } @Override public boolean prePostHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { if (logger.isDebugEnabled()) { logger.debug("PublishUpdateInterceptor prePostHandle"); } return true; } }
本例中使用一个独立的文件applicationContext-interceptors.xml用于描述工程中的拦截器,在web.xml中配置解析该配置文件。
applicationContext-interceptors.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <mvc:interceptors> <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 --> <!-- <bean class="com.arvinfei.springmvc.DemoInterceptor"/> --> <mvc:interceptor> <mvc:mapping path="/manage/test.shtml"/> <pre name="code" class="html"> <mvc:mapping path="/manage/test2.shtml"/><!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 --> <bean class="com.arvinfei.springmvc.DemoInterceptor"/> </mvc:interceptor> </mvc:interceptors> </beans>
上面的配置DemoInterceptor将拦截/manage/test.shtml和/manage/test.shtml的post请求。
web.xml中指定所有以applicationContext-开头的配置文件,主要内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.shtml</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
关于SpringMVC中拦截器的更多较为详细的介绍可以参考如下博客:
http://itindex.net/detail/50153-springmvc-interceptor