Spring MVC Interceptor

1 在spring-servlet.xml中进行如下配置

<mvc:interceptors>
        <mvc:interceptor> 
            <mvc:mapping path="/saveObjectInstance.do"/>  
            <mvc:mapping path="/createObjectInstance.do"/>  
            <mvc:mapping path="/cloneObjectInstance.do"/> 
            <bean class="com.hp.it.qdpadmin.web.interceptor.ValidationInterceptor" /> 
        </mvc:interceptor>
</mvc:interceptors>

说明:

* path 为被拦截的controller请求的url,可以配置多个

* bean class 为自定义的Interceptor

* 拦截器可以配置多个,按先后顺序进行拦截

2 定义Interceptor class 实现HandlerInterceptor

public class ValidationInterceptor implements HandlerInterceptor {

    private final Logger logger = Logger.getLogger(this.getClass());

    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {

    }

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object arg2) throws Exception {
        //do validation
               return false;
       }
}
posted @ 2015-02-10 12:18  风过无痕的博客  阅读(341)  评论(0编辑  收藏  举报