日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

dwr工具類

張軍 9529 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.sfpk123.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.sfpk123.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.sfpk123.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條評論
主站蜘蛛池模板: 呼玛县| 营口市| 高陵县| 江口县| 高淳县| 志丹县| 安龙县| 习水县| 烟台市| 阳东县| 玉林市| 井研县| 大竹县| 冷水江市| 通海县| 北票市| 温宿县| 伊川县| 怀来县| 牟定县| 星座| 卫辉市| 龙山县| 阿巴嘎旗| 新化县| 鲁山县| 广水市| 甘孜县| 休宁县| 治县。| 西林县| 惠水县| 普兰县| 喀喇| 涞水县| 海淀区| 宁德市| 扎兰屯市| 兴安县| 武陟县| 布尔津县|