拦截器preHandle()不会重定向到login.html

我有一个弹簧应用程序。 我引入了一个sessionInterceptor来阻止对index.jsp的直接访问。 如果用户未登录,则无法访问index.jsp,应将其重定向到login.html。 代码正在执行preHandle()方法并运行所有代码,但在return false它不会重定向到login.html。 怎么了? 有没有大师帮忙? 提前致谢。

我在SessionInterceptor.java中的preHandle()是:

 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); // if displaying the home page, make sure the user is reloaded. if (request.getRequestURI().endsWith("login.html")) { session.removeAttribute("isUserLoggedIn"); } if (session.getAttribute("isUserLoggedIn") == null && !request.getRequestURI().endsWith("login")) { response.sendRedirect(request.getContextPath() + "/login.html"); return false; } return true; } 

我也试过以下但都是徒劳的。

 RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/login.html"); dispatcher.forward(request, response); 

我的dispatcher-servlet.xml设置是:

                 

web.xml是:

   dispatcher /rest/*   login.html  

您可以尝试重定向到将从控制器捕获的逻辑路径尝试

 response.sendRedirect("/NotLogged"); 

然后创建一个这样的函数:

 @RequestMapping(value = {"/NotLogged"}, method = RequestMethod.GET) public String notLogged() { return "login.html"; } 

我希望它对你有用!