springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面,廢話不多少了,具體如下:
第一步,配置 web.xml
1
2
3
|
< session-config > < session-timeout >15</ session-timeout > </ session-config > |
第二步,配置spring-mvc.xml
1
2
3
4
5
6
7
8
9
10
11
|
<!-- 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 方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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
1
2
3
4
5
6
7
8
|
<body> <SCRIPT language= "JavaScript" > alert( "用戶已在其他地方登陸,請重新登錄。" ); setTimeout( function () { window.top.location.href= "<%=path%>/index.jsp" ; },2000); </script> </body> |
到此 springMvc攔截session失效后處理方式結(jié)束。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/fiangasdre/article/details/51802625