dwr工具类

张军 6304 0

所有工具类

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>&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;<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>
----------------------------------	



更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论