最简单的如下面,一个方法就搞定了。
function errh(errorString, exception) { alert("提示你自定义的全局出错信息。"); } dwr.engine.setErrorHandler(errh);当然我在项目中是类似这么处理的:
public class ErrDemo { public String getData() { Date when = new Date();//FooException extend Exception throw new FooException("异常的详细信息", when); //FooException 实现getWhen() method } }如果不设置FooException的Message,默认是“Error”。
function eh(msg, ex) { alert(msg + ", date=" + ex.when); //alertMessagesBox('alert-messages-box',"" + dwr.util.toDescriptiveString(exc, 2),true,msg) ; } DWREngine.setErrorHandler(eh);
<convert match="com.db.exception.FooException" converter="exception"> <param name="include" value="message"/> </convert>当执行 ErrDemo.getData(function(data) { alert("123"); });时,出现alert("异常的详细信息, date=xxxxxxxxxx");
Demo.method(params, { callback:function(data) { alert("it worked"); }, errorHandler:function(message) { alert("it broke"); }, timeout:1000 });对某一个function来进行超时\警告\错误处理。
DWREngine.setErrorHandler(function)对错误的处理 DWREngine.setWarningHandler(function)对警告的处理 DWREngine.setTimeout() 设置超时的时间 DWREngine.setAsync(false);设置DWR为同步调用 DWREngine.setAsync(true);设置DWR为异步调用
创建一个DWRSessionService.java
public class DWRSessionService { public String query(String param,HttpSession session) throws Exception { logger.info(param); if (session.getAttribute(Constants.SESSION_USER) == null) { WebContext ctx = WebContextFactory.get(); Util utilThis = new Util(ctx.getScriptSession()); utilThis.addScript(new ScriptBuffer("logout()")); //ctx.getHttpServletResponse().sendRedirect(ctx.getHttpServletRequest().getContextPath()+"/login/login"); return null; } else { Map queryMap = new HashMap(); queryMap.put("obj", param); //List<Map> listIis1 = reportDao.select4MapParam(queryMap, "currentSql"); List<Map> listIis2 =reportDao.select4MapParam(queryMap,"test123"); return JSONArray.fromObject(listIis2).toString(); } } }在页面上(当然最好是加在公共的header.jsp里,这样只需要加一处就ok了)
function logout(){ alertDialog("Session timeout",SESSION_TIMEOUT_MSG,'alert-messages-box',function(){ window.location.href = "<%=request.getContextPath() %>/wms/login.jsp"; }); }这样就大功告成了。
参考:http://blog.csdn.net/wangchsh2008/article/details/6652809