struts 2中當(dāng)SESSION過(guò)期后返回登陸頁(yè)面
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-02-20 03:45:23 閱讀次數(shù):3529次
在struts2+spring+hibernate 的體系中,經(jīng)常要遇到的一個(gè)問(wèn)題就是當(dāng)用戶(hù)登陸后,SESSION超過(guò)指定的時(shí)間后,
當(dāng)用戶(hù)再點(diǎn)某個(gè)連接時(shí),系統(tǒng)會(huì)自動(dòng)返回到登陸的頁(yè)面。在實(shí)現(xiàn)時(shí)候,由于是STRUTS2,因此可以充分利用攔截器。
攔截器示例代碼如下
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
Action action = (Action)actionInvocation.getAction();
if (action instanceof LoginSystem) {
return actionInvocation.invoke();
}
String userName = (String)session.get(USER_NAME);
if(userName == null)
{
return LOGIN;
} else {
return actionInvocation.invoke();
}
要注意的,由于要排除登陸這個(gè)ACTION,所以要用 if (action instanceof LoginSystem) { 這句去判斷,
因?yàn)榈顷懬皶?huì)執(zhí)行這個(gè)攔截器,這時(shí)SESSION肯定為空,就不能再跑去顯示“SESSSION超時(shí)”這個(gè)頁(yè)面了。
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)