项目使用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时,弹出提示信息,并且中止执行其他其他操作,不再执行回调函数。