所有工具类
DWR包含两个主要部分:
1. 运行在服务器端的servlet控制器(DwrServlet),它负责接收请求,调用相应业务逻辑进行处理,向客户端返回响应。
2.运行在浏览器端的JavaScript,它负责向服务器端发送请求,接收响应,动态更新页面
zj.dwr.util.DwrUtil
package zj.dwr.util;
import java.util.List;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.proxy.dwr.Util;
import zj.dwr.bean.DwrBean;
import zj.dwr.bean.DwrSession;
/**
* dwr工具类<br>
*
* @version 1.00 (2011/11/08)
* @author 张军 {@link <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>}
*/
public class DwrUtil {
/**
* 发送数据至前台
*
* @param funName
* 函数名
* @param value
* 函数值
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public final static <T> void sendAllMessage(String funName, T value) {
for (String key : DwrSession.SCRIPT_SESSIONS.keySet()) {
sendMessage(key, funName, value);
}
}
/**
* 向前台推送消息
*
* @param dwrBean
* dwr参数
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public final static <T> void sendAllMessage(DwrBean<T> dwrBean) {
List<String> funNames = dwrBean.getFunNameList();
for (String key : DwrSession.SCRIPT_SESSIONS.keySet()) {
ScriptSession scriptSession = DwrSession.SCRIPT_SESSIONS.get(key);
if (scriptSession != null) {
Util util = new Util(scriptSession);
if (funNames != null && funNames.size() > 0)
for (String funName : funNames) {
util.addFunctionCall(funName, dwrBean.getTvalue());
}
}
}
}
/**
* 向前台推送单个key消息
*
* @param key
* 注册键
* @param dwrBean
* dwr参数
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public final static <T> void sendMessage(String key, DwrBean<T> dwrBean) {
List<String> funNames = dwrBean.getFunNameList();
ScriptSession scriptSession = DwrSession.SCRIPT_SESSIONS.get(key);
if (scriptSession != null) {
Util util = new Util(scriptSession);
if (funNames != null && funNames.size() > 0)
for (String funName : funNames) {
util.addFunctionCall(funName, dwrBean.getTvalue());
}
}
}
/**
* 向前台推送单个key消息
*
* @param key
* 注册键
* @param funName
* 函数名
* @param value
* 函数值
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
*/
public final static <T> void sendMessage(String key, String funName, T value) {
ScriptSession scriptSession = DwrSession.SCRIPT_SESSIONS.get(key);
if (scriptSession != null) {
Util util = new Util(scriptSession);
util.addFunctionCall(funName, value);
}
}
}zj.dwr.bean.DwrBean<T>
package zj.dwr.bean;
import java.io.Serializable;
import java.util.List;
/**
* dwr封装类<br>
*
* @version 1.00 (2011/11/08)
* @author 张军 {@link <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>}
*/
public class DwrBean<T> implements Serializable {
private static final long serialVersionUID = 1L;
private List<String> funNameList;
private T tvalue;
/**
* 调用前台的函数名
*
* @Description
* @author 张军
* @date 2016-8-2 下午9:41:30
* @version V1.0
* @return 调用前台的函数名
*/
public List<String> getFunNameList() {
return funNameList;
}
/**
* 调用前台的函数名
*
* @Description
* @param funNameList
* 调用前台的函数名
* @author 张军
* @date 2016 下午9:41:30
* @modifiyNote
* @version 1.0
*/
public void setFunNameList(List<String> funNameList) {
this.funNameList = funNameList;
}
/**
* 传给前台的值
*
* @Description
* @author 张军
* @date 2016-8-2 下午9:41:59
* @version V1.0
* @return 传给前台的值
*/
public T getTvalue() {
return tvalue;
}
/**
* 传给前台的值
*
* @Description
* @param tvalue
* 传给前台的值
* @author 张军
* @date 2016 下午9:41:59
* @modifiyNote
* @version 1.0
*/
public void setTvalue(T tvalue) {
this.tvalue = tvalue;
}
}zj.dwr.bean.DwrSession
package zj.dwr.bean;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import zj.check.util.CheckUtil;
/**
* dwr注册类<br>
*
* @version 1.00 (2011/11/08)
* @author 张军 {@link <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>}
*/
public final class DwrSession implements Serializable {
private static final long serialVersionUID = 1L;
private transient static final Logger log = Logger.getLogger(DwrSession.class);
/** Dwr内存注册 **/
public static final Map<String, ScriptSession> SCRIPT_SESSIONS = Collections.synchronizedMap(new HashMap<String, ScriptSession>());
/**
* 注册session
*
* @param key
*/
public final void regScriptSession(String key) {
try {
WebContext webContext = WebContextFactory.get();
/** 获取页面长连接session */
ScriptSession scriptSession = webContext.getScriptSession();
// WebSession webSession = GlobalOperation.getWebSession(webContext.getSession().getId());
// if (webSession != null) {
// User user = webSession.getUser();
// // 用户工号+长连接标识=scriptSession唯一标识
// // scriptSession保存内存中
// MemoryConstant.ssMapScriptSession.put(key, scriptSession);
// }
// // 获取当前页面URL,比如/ext3/test_tag.jsp
// String currentPage = webContext.getCurrentPage();
// // 获取所有浏览当前页面的脚本session
// Collection<ScriptSession> sessions = webContext.getScriptSessionsByPage(currentPage);
// System.out.println("页面数:" + sessions.size());
if (CheckUtil.isNotNull(key)) {
SCRIPT_SESSIONS.put(key, scriptSession);
log.info("注册ScriptSession成功【" + SCRIPT_SESSIONS.size() + "】:" + key);
}
} catch (Exception e) {
log.error("注册ScriptSession失败", e);
}
}
/**
* 注册session
*
* @param key
*/
public final void removeScriptSession(String key) {
try {
SCRIPT_SESSIONS.remove(key);
log.info("移除ScriptSession成功【" + SCRIPT_SESSIONS.size() + "】:" + key);
} catch (Exception e) {
log.error("移除ScriptSession失败", e);
}
}
/**
* 错误信息
*
* @param errorMessage
* @param scriptURL
* @param lineNumber
*/
public final void writeJsError(String errorMessage, String scriptURL, String lineNumber) {
if (CheckUtil.isNotNull(scriptURL))
log.error("scriptURL:" + scriptURL);
if (CheckUtil.isNotNull(errorMessage))
log.error("errorMessage:" + errorMessage);
if (CheckUtil.isNotNull(lineNumber))
log.error("lineNumber:" + lineNumber);
}
}dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create creator="spring" javascript="dwrUtil"> <param name="beanName" value="dwrUtil" /> </create> <convert converter="bean" match="com.zjsystem.basic.permissions.notices.pageModel.NoticePage" /> <convert converter="bean" match="com.zjsystem.framework.dwr.bean.DwrMsg" /> <convert converter="exception" match="java.lang.Exception" /> <convert converter="bean" match="java.lang.StackTraceElement" /> <convert converter="bean" match="zj.common.KV" /> </allow> </dwr>
总结
<script type="text/javascript" src="${webRoot }/dwr/engine.js"></script>
<script type="text/javascript" src="${webRoot }/dwr/util.js"></script>
<script type="text/javascript" src="${webRoot }/dwr/interface/dwrUtil.js"></script>
<input type="hidden" class="zj-sid" style="width: 200px" />
<script type="text/javascript" charset="utf-8">
$(function() {
/**
onUnload方法是在关闭窗口之后执行
onbeforeUnload方法是在关闭窗口之前执行
window.onbeforeunload= function(event) { return confirm("确定离开此页面吗?"); }
window.onunload = function(event) { return confirm("确定离开此页面吗?"); }
**/
dwr.engine.setActiveReverseAjax(true);
var uuid = "zj-" + jq.UUID();
dwrUtil.regScriptSession(uuid);
$(".zj-sid").val(uuid);
});
//退出时删除
window.onbeforeunload = function(event) {
//删除后台session
dwrUtil.removeScriptSession($(".zj-sid").val());
}
</script>
// 发送数据至前台
for (String key : DwrSession.SCRIPT_SESSIONS.keySet()) {
if (key.startsWith("zj-")) {
ScriptSession scriptSession = DwrSession.SCRIPT_SESSIONS.get(key);
if (scriptSession != null) {
Util util = new Util(scriptSession);
util.addFunctionCall("zj_dwr", lkv);
}
}
}
web.xml
----------------------------------
<!-- Servlet add by zhangjun start -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!-- 前台输入地址是否显示 -->
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>pollAndCometEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>使用服务器推技术(反转AJAX)</description>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>scriptSessionTimeout</param-name>
<param-value>21600000</param-value>
</init-param>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>100</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- Servlet add by zhangjun start -->
----------------------------------
WEB-INF/dwr.xml
----------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="spring" javascript="dwrUtil">
<param name="beanName" value="dwrUtil" />
</create>
<convert converter="bean" match="com.zjsystem.basic.permissions.notices.pageModel.NoticePage" />
<convert converter="bean" match="com.zjsystem.framework.dwr.bean.DwrMsg" />
<convert converter="exception" match="java.lang.Exception" />
<convert converter="bean" match="java.lang.StackTraceElement" />
<convert converter="bean" match="zj.common.KV" />
</allow>
</dwr>
----------------------------------
本文为张军原创文章,转载无需和我联系,但请注明来自张军的军军小站,个人博客http://www.zhangjunbk.com

