DWR的异常处理及session过期
来源:互联网 发布:宾得镜头 知乎 编辑:程序博客网 时间:2024/05/02 01:40
DWR的后台server调用可能会产生Exception,如果不处理,前台的反应就是页面js报错了。这对于用户是很不友好的。
最近这个项目里的DWR异常处理,是参考网友的经验做的。下面简单谈谈:
DWR有很多种异常处理方式,有全局的,有针对某种方法的。
先说全局处理:
最简单的如下面,一个方法就搞定了。
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);
DWR.toDescriptiveString()是DWR util.js的一个函数。
带debug信息的toString,第一个为将要debug的对象,第二个参数为处理等级。等级如下:
0: Single line of debug 单行调试
1: Multi-line debug that does not dig into child objects 不分析子元素的多行调试
2: Multi-line debug that digs into the 2nd layer of child objects 最多分析到第二层子元素的多行调试
在dwr.xml里面配置
<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来进行超时\警告\错误处理。
下面列出engine.js中一些常用方法:
DWREngine.setErrorHandler(function)对错误的处理DWREngine.setWarningHandler(function)对警告的处理DWREngine.setTimeout() 设置超时的时间 DWREngine.setAsync(false);设置DWR为同步调用DWREngine.setAsync(true);设置DWR为异步调用
最后说说Session超时的情况
使用DWR,还需要考虑session超时的情况,当session超时了,系统应该友好地提示用户,并跳转到login页面。
网上有大致由两种处理方法,我在项目用了其中一种,如下:
首先在web.xml的dwr配置中(DwrServlet)加入
<init-param><param-name>org.directwebremoting.extend.Remoter</param-name><param-value>com.sch.common.DWRSessionService</param-value></init-param>创建一个DWRSessionService.java
/** * @(#)DWRSessionService.java Aug 8, 2011 */package com.sch.common;import javax.servlet.http.HttpServletRequest;import org.apache.log4j.Logger;import org.directwebremoting.ScriptBuffer;import org.directwebremoting.WebContext;import org.directwebremoting.WebContextFactory;import org.directwebremoting.extend.Calls;import org.directwebremoting.extend.Replies;import org.directwebremoting.impl.DefaultRemoter;import org.directwebremoting.proxy.dwr.Util;/** * desc: comment * @author Chaisson * @since Aug 8, 2011 11:11:30 AM * @vision 1.0 */public class DWRSessionService extends DefaultRemoter {Logger logger = Logger.getLogger(this.getClass());public Replies execute(Calls calls) {HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();// check sessionif (req.getSession(false) == null) {doLogout();return super.execute(new Calls());}return super.execute(calls);}private void doLogout() {logger.debug("dwr session is timeout");WebContext wct = WebContextFactory.get();Util utilThis = new Util(wct.getScriptSession());utilThis.addScript(new ScriptBuffer("logout()"));}}在页面上(当然最好是加在公共的header.jsp里,这样只需要加一处就ok了)
function logout(){ alertDialog("Session timeout",SESSION_TIMEOUT_MSG,'alert-messages-box',function(){ window.location.href = "<%=request.getContextPath() %>/wms/login.jsp"; });}这样就大功告成了。
注:session处理方法参考http://blog.163.com/zjut_jing/blog/static/131136457200910481526388/
- DWR的异常处理及session过期
- DWR的异常处理及session过期
- DWR session过期处理方案
- DWR session过期处理方案
- dwr调用时session过期处理
- DWR 控制请求session过期处理
- Asp.net MVC Session过期异常的处理
- dwr之session过期
- dwr的异常处理
- dwr的异常处理
- dwr接受session过期提示
- DWR 异常SESSION ERROR
- dwr 出现session error错误的处理
- Extjs 处理Session过期的方法
- 关于session过期跳转的处理方法
- Ajax 请求session过期的统一处理
- Tomcat的Session过期处理策略
- Android处理登录的session过期
- NIO中异步IO的完整实现实例
- 研究牛人的程序struct-dump.h
- 黑马程序员--javaAPI
- linux查看硬件信息及驱动设备相关整理(留存验证)
- Android 获取自己程序APK包的MD5指纹
- DWR的异常处理及session过期
- Ubuntu桌面版讨论使用全局菜单和取消的命令
- 入侵软件学习
- sql 连接字符串大全
- 2003 计划任务
- poj 1511 Invitation Cards (SPFA、邻接表)
- 分布式 [ mpi 安装 与 执行]
- oracle tnsnames 配置文件
- Server 2008 下AjaxPro JS未定义错误处理