1. The springmvc filter request.getRequestDispatcher jumped, and the log also indicated that the view (jsp) was accessed, but the browser did not move.
if(!isLogin){
httpServletRequest.getRequestDispatcher("/toLogin").forward(httpServletRequest, httpServletResponse);
}
17:57:55.062 [http-nio-8080-exec-3] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'indexLogin'
17:57:55.062 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/toLogin] is: -1
17:57:55.063 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'login'; URL [/WEB-INF/view/login.jsp]] in DispatcherServlet with name 'springMVC'
17:57:55.063 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/view/login.jsp] in InternalResourceView 'login'
17:57:55.064 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
There is a function request on the page to request the controller through jquery ajax. The filter finds that there is no login. The filter request.getRequestDispatcher initiates a login request and jumps to the login page. The log record has been forwarded (see Figure 2), but the browser does not jump.
2. And it’s no problem to request /toLogin alone, just come out to the login page
When making an AJAX request, the page you return will be used as data using
XMLHttpRequest.responseText
to receive it.The correct approach should be that if the background finds that the conditions for successful login are not met, return an unsuccessful flag to AJAX (for example:
{"login" : "failed"}
), and then the front-end determines if it is this flag, then passwindow.location.href
Jump to the "/toLogin" page you defined.Ajax jumps using js, and form submission uses your method