springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁(yè)面,廢話不多少了,具體如下:
第一步,配置 web.xml
<session-config> <session-timeout>15</session-timeout> </session-config>
第二步,配置spring-mvc.xml
<!-- Session失效攔截 --> <mvc:interceptors> <!-- 定義攔截器 --> <mvc:interceptor> <!-- 匹配的是url路徑, 如果不配置或/**,將攔截所有的Controller --> <mvc:mapping path="/**" /> <!-- 不需要攔截的地址 --> <mvc:exclude-mapping path="/login.do" /> <bean class="com.cm.contract.controller.annotation.GEISSSessionTimeoutInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
第三步,寫(xiě)攔截器SystemSessionInterceptor 方法
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); //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; }第五步,配置友情提示頁(yè)面sessionrun.jsp
<body> <SCRIPT language="JavaScript"> alert("用戶已在其他地方登陸,請(qǐng)重新登錄。"); setTimeout(function () { window.top.location.href="<%=path%>/index.jsp"; },2000); </script> </body> 到此 springMvc攔截session失效后處理方式結(jié)束。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選