`
蛤蟆仙人
  • 浏览: 114378 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring mvc 拦截器

阅读更多

拦截器配置:

 

 <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.com.xxx.interceptor.UserAuthInterceptor" />
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.com.xxx.interceptor.DBSwitchInterceptor" />
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.com.xxx.interceptor.PermissionInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

 描述:

1.拦截器按照先后顺序进行拦截;

2.拦截全部请求时使用"/**";

3.拦截只针对controller.

 

拦截器要实现HandlerInterceptor

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class DBSwitchInterceptor implements HandlerInterceptor {
    
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器工作。。。。");
        return true;
    }
    
    @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 {
        
    }
}
 

 

分享到:
评论
1 楼 命中注定1314 2012-09-11  
还可以用HandlerInterceptor Adapter吧

相关推荐

Global site tag (gtag.js) - Google Analytics