1.实现HandlerInterceptor接口
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { // 获取HttpSession对象 HttpSession session = request.getSession(); // 判断session中是否有登录信息 if (session.getAttribute("id") == null) { // 没有登录信息,则重定向到登录页 System.err.println("拦截器启动+重定向"); response.sendRedirect("../"); // 执行拦截 return false; } // 放行 return true; }
2.配置springmvc.xml
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/拦截地址/" /> <bean class="com.high.interceptor.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors>