SpringMvc controls login user session to jump to the login page after the login page is invalid. There is not much nonsense. The details are as follows:
The first step is to configure web.xml
<session-config> <session-timeout>15</session-timeout> </session-config>
The second step is to configure spring-mvc.xml
<!-- Session failure intercept--> <mvc:interceptors> <!-- Definition of interceptor--> <mvc:interceptor> <!-- Matching the url path. If not configured or /**, all controllers will be intercepted --> <mvc:mapping path="/**" /> <!-- Addresses that do not need to be intercepted--> <mvc:exclude-mapping path="/login.do" /> <bean></bean> </mvc:interceptor> </mvc:interceptors>
Step 3: Write the Interceptor SystemSessionInterceptor method
public class SystemSessionInterceptor implements HandlerInterceptor { private static final String LOGIN_URL="/jsp/sessionrun.jsp"; @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session=request.getSession(true); //Get username information in session Object obj = session.getAttribute(CMConstant.LOGINUSER); if (obj==null||"".equals(obj.toString())) { response.sendRedirect(request.getSession().getServletContext().getContextPath()+LOGIN_URL; return false; } return true; } Step 5: Configure the friendly prompt page sessionrun.jsp
<body> <SCRIPT language="JavaScript"> alert("The user has logged in elsewhere, please log in again."); setTimeout(function () { window.top.location.href="<%=path%>/index.jsp"; },2000); </script> </body> The processing method after the springMvc intercept session expires is ended.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.