springmvc控制登錄用戶session失效后跳轉登錄頁面,廢話不多少了,具體如下:
第一步,配置 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>
第三步,寫攔截器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;
}
第五步,配置友情提示頁面sessionrun.jsp
<body>
<SCRIPT language="JavaScript">
alert("用戶已在其他地方登陸,請重新登錄。");
setTimeout(function () {
window.top.location.href="<%=path%>/index.jsp";
},2000);
</script>
</body>
到此 springMvc攔截session失效后處理方式結束。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
<div class="art_xg">
