码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC Interceptor

时间:2015-02-10 13:13:58      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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;
       }
}

Spring MVC Interceptor

标签:

原文地址:http://www.cnblogs.com/tangyanbo/p/4283515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!