1. 程式人生 > 實用技巧 >ssm 攔截器的使用

ssm 攔截器的使用

1、建立攔截器

public class PowerInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // TODO Auto-generated method stub

        HttpSession session = request.getSession();

        Myemp emp 
= (Myemp) session.getAttribute(Constrains.LOGIN_USER); if (emp == null) { response.sendRedirect(request.getContextPath()+"/index.jsp"); return false; } return super.preHandle(request, response, handler); } }

2、添加註解

<mvc:interceptors>
        <
mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/login"/> <mvc:exclude-mapping path="/js/**"/> <mvc:exclude-mapping path="/css/**"/> <mvc:exclude-mapping path="/font/**"/> <mvc:exclude-mapping
path="/images/**"/> <mvc:exclude-mapping path="/layer/**"/> <mvc:exclude-mapping path="/layui/**"/> <mvc:exclude-mapping path="/login"/> <bean class="com.cc.house.web.interceptor.PowerInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>