国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > AJAX > 正文

Ajax Session失效如何跳轉(zhuǎn)登錄頁面

2024-09-01 08:30:34
字體:
供稿:網(wǎng)友

我們在Struts應(yīng)用中經(jīng)常會經(jīng)過相應(yīng)的攔截器進行相關(guān)處理,一般請求的時候Session失效時就會跳到登錄頁面,那么你知道Ajax Session失效如何跳轉(zhuǎn)登錄頁面嗎?

一、建立攔截器

package com.xxx.planeap.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import com.xxx.common.contants.ConstantsKey;import com.xxx.common.contants.SessionKey;import com.xxx.planeap.domain.User;import com.xxx.planeap.security.SecurityContextUtil;/*** * @author Goma OMA1989@YEAH.NET* @version v1.0* @since 2012-05-31* */public class SecurityInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 1L;private Logger logger = Logger.getLogger(SecurityInterceptor.class);@Overridepublic String intercept(ActionInvocation invocation) throws Exception {// TODO Auto-generated method stubString className = invocation.getAction().getClass().getName();String action = className.substring(className.lastIndexOf(".")+1,className.length());String actionName = invocation.getProxy().getActionName();String result;HttpServletRequest request = ServletActionContext.getRequest();HttpServletResponse response = ServletActionContext.getResponse();String type = request.getHeader("X-Requested-With");User user = (User) ActionContext.getContext().getSession().get(SessionKey.CURRENT_USER);if (user == null) {logger.debug("SECURITY CHECKED: NEED TO LOGIN");if ("XMLHttpRequest".equalsIgnoreCase(type)) {// AJAX REQUEST PROCESSresponse.setHeader("sessionstatus", ConstantsKey.MSG_TIME_OUT);result = null;} else {// NORMAL REQUEST PROCESSresult = ActionSupport.LOGIN;}} else {logger.debug("SECURITY CHECKED: USER HAS LOGINED");SecurityContextUtil.setCurrentUser(user);boolean hanPerm = SecurityContextUtil.hasPerm(action, actionName);logger.debug("SECURITY CHECKED: PERMISSION---"+action+"."+actionName+"="+hanPerm);result = invocation.invoke();}return result;}}

二、定義全局AJAX請求結(jié)束處理方法

//全局的AJAX訪問,處理AJAX清求時SESSION超時$.ajaxSetup({contentType:"application/x-www-form-urlencoded;charset=utf-8",complete:function(XMLHttpRequest,textStatus){//通過XMLHttpRequest取得響應(yīng)頭,sessionstatus var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); if(sessionstatus=="timeout"){//這里怎么處理在你,這里跳轉(zhuǎn)的登錄頁面window.location.replace(PlanEap.getActionURI("login"));}}});

也就是ajax發(fā)送請求時如果攔截返回一個表示就跳轉(zhuǎn),否則執(zhí)行正常操作。

以上就是小編介紹Ajax Session失效如何跳轉(zhuǎn)登錄頁面的內(nèi)容,需要的朋友趕緊動手嘗試一下吧,這在我們?nèi)蘸笫褂糜嬎銠C的過程中會帶來不少方便。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 军事| 四会市| 榆中县| 监利县| 尉犁县| 英德市| 婺源县| 连江县| 乌什县| 平定县| 清流县| 甘谷县| 亳州市| 乐亭县| 那坡县| 兴义市| 东乡县| 郯城县| 唐河县| 凌源市| 阳信县| 武定县| 讷河市| 繁昌县| 饶河县| 赣榆县| 扬州市| 成都市| 岱山县| 文水县| 越西县| 交口县| 成武县| 迁安市| 任丘市| 阿拉善左旗| 长阳| 桦川县| 焉耆| 宣汉县| 庐江县|