dwr方法调用session过期问题的解决方法

项目使用Ext作为表现层,由dwr异步调用义务逻辑方法,通过filter过滤请求来实现权限管理,通过检查session中是否有User对象来判断是否为登陆用户。 这样做出现一个问题,当session过期时,异步调用方法无法得到相应的提示。

于是要通过对dwr.engine 进行修改来实现session 过期时有适当的提示信息。要点如下:

1. 在web.xml配置filter,对dwr方法调用路径进行拦截,url-partern(*.dwr);

    在filter中编写代码,当session中有User对象时放行,session没有User对象时,执行response.setStatus(1000); 1000使我们自己指定的状态码。

2. 在jsp页面中引入dwr的js文件后执行如下js代码:

  

 

      dwr.engine._stateChange = function(batch) {
      var toEval;
     
      if (batch.completed) {
        dwr.engine._debug("Error: _stateChange() with batch.completed");
        return;
      }
    
      var req = batch.req;
      try {
        if (req.readyState != 4) return;
      }
      catch (ex) {
        dwr.engine._handleWarning(batch, ex);
        // It's broken - clear up and forget this call
        dwr.engine._clearUp(batch);
        return;
      }
    
      try {
        var reply = req.responseText;
        reply = dwr.engine._replyRewriteHandler(reply);
        var status = req.status; // causes Mozilla to except on page moves
    
        if (reply == null || reply == "") {
          dwr.engine._handleWarning(batch, { name:"dwr.engine.missingData", message:"No data       received from server" });
        }
        else if (status != 200) {
          if ( status == 1000) {
           alert("未登录或登录超时,请重新登录");
           return;
          }
          dwr.engine._handleError(batch, { name:"dwr.engine.http." + status,     message:req.statusText });
        }
        else {
          var contentType = req.getResponseHeader("Content-Type");
          if (!contentType.match(/^text\/plain/) && !contentType.match(/^text\/javascript/)) {
            if (contentType.match(/^text\/html/) && typeof batch.textHtmlHandler == "function") {
              batch.textHtmlHandler();
            }
            else {
              dwr.engine._handleWarning(batch, { name:"dwr.engine.invalidMimeType", message:"Invalid content type: '" + contentType + "'" });
            }
          }
          else {
            // Comet replies might have already partially executed
            if (batch.isPoll && batch.map.partialResponse == dwr.engine._partialResponseYes) {
              dwr.engine._processCometResponse(reply, batch);
            }
            else {
              if (reply.search("//#DWR") == -1) {
                dwr.engine._handleWarning(batch, { name:"dwr.engine.invalidReply", message:"Invalid reply from server" });
              }
              else {
                toEval = reply;
              }
            }
          }
        }
      }
      catch (ex) {
        dwr.engine._handleWarning(batch, ex);
      }
    
      dwr.engine._callPostHooks(batch);
    
      // Outside of the try/catch so errors propogate normally:
      dwr.engine._receivedBatch = batch;
      if (toEval != null) toEval = toEval.replace(dwr.engine._scriptTagProtection, "");
      dwr.engine._eval(toEval);
      dwr.engine._receivedBatch = null;
    
      dwr.engine._clearUp(batch);
    };
      

其中红色的代码是我们添加的,其余为engine.js的源代码,我们状态为我们制定的1000时,弹出提示信息,并且中止执行其他其他操作,不再执行回调函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值