今天無意間看到了一篇關(guān)于dwr的文章,講的是反向Ajax,想了想半年前一個(gè)項(xiàng)目的功能需求如果用它實(shí)現(xiàn)就太完美了。
項(xiàng)目要求是:數(shù)據(jù)庫中A表中如果有了新的數(shù)據(jù),系統(tǒng)的管理界面上要實(shí)時(shí)的顯示出來,提醒給操作人員(是某種報(bào)警信息,好像特別重要)。
項(xiàng)目要求是:數(shù)據(jù)庫中A表中如果有了新的數(shù)據(jù),系統(tǒng)的管理界面上要實(shí)時(shí)的顯示出來,提醒給操作人員(是某種報(bào)警信息,好像特別重要)。
當(dāng)時(shí)的實(shí)現(xiàn)方法是:讓一Iframe里的頁面每隔多少秒就刷新一次,來監(jiān)視數(shù)據(jù)庫A表的變化。這樣客戶端不斷地向服務(wù)發(fā)送請(qǐng)求,服務(wù)器被動(dòng)的返回客戶端想要的數(shù)據(jù)。
反向Ajax技術(shù)是服務(wù)器向客戶端主動(dòng)的發(fā)關(guān)數(shù)據(jù),很大的減少了服務(wù)的負(fù)擔(dān)。
學(xué)習(xí)新的知識(shí)點(diǎn)就要多寫些自己的Demo,在這里寫一個(gè)最簡(jiǎn)單的例子,以作筆記:
sendMsg.jsp負(fù)責(zé)向表中添加數(shù)據(jù)(更新數(shù)據(jù)庫),showMsg.jsp用來實(shí)時(shí)顯示前者添加的信息。
dwr.xml
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr//dwr30.dtd"> <dwr>
<allow>
<create creator="new" javascript="SendMsg" scope="session">
<param name="class" value="com.dwr.bean.SendMsg" />
</create>
</allow>
</dwr>
<allow>
<create creator="new" javascript="SendMsg" scope="session">
<param name="class" value="com.dwr.bean.SendMsg" />
</create>
</allow>
</dwr>
SendMsg.java
package com.dwr.bean;
import java.util.Collection;
import java.util.LinkedList;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
/**
* DWR反向Ajax示例
* @author ︶ㄣ旺
* @version 1.0
* */
public class SendMsg
{
public static WebContext wctx = null;
//用一個(gè)List代表數(shù)據(jù)庫 來儲(chǔ)存消息
private LinkedList list = new LinkedList();
//調(diào)用添加和顯示方法
public void sendMsg(String msg)
{
list.addFirst(msg);
//最多保留10條聊天記錄
if(list.size()>10){
list.removeLast();
}
wctx = WebContextFactory.get();
Util utilThis = new Util(wctx.getScriptSession());
//使用utilThis重置 Id 屬性為 msg 的文本框的內(nèi)容
utilThis.setValue("msg", "請(qǐng)輸入信息");
String currentPage = "/login/showMsg.jsp"; //要推信息的頁面地址
//獲得所有已經(jīng)打開此頁面的會(huì)話
Collection sessions = wctx.getScriptSessionsByPage(currentPage);
Util utilAll = new Util(sessions);
//將消息從LinkedList中取出來,放放到一個(gè)字符串?dāng)?shù)組中
//在這里遇到一個(gè)問題,就是直接用String[] msgs = (String[])list.toArray();時(shí),報(bào)類型轉(zhuǎn)換錯(cuò)誤
//但如下,給toArray()方法傳遞一個(gè)空的數(shù)組就不報(bào)錯(cuò)了,求解中
.
String[] msgs = new String[list.size()];
msgs = (String[]) list.toArray(msgs);
//先清空頁面的消息內(nèi)容,去除ul元素下所有元素
utilAll.removeAllOptions("ul");
//向頁面添加消息內(nèi)容
utilAll.addOptions("ul", msgs);
}
}
import java.util.Collection;
import java.util.LinkedList;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
/**
* DWR反向Ajax示例
* @author ︶ㄣ旺
* @version 1.0
* */
public class SendMsg
{
public static WebContext wctx = null;
//用一個(gè)List代表數(shù)據(jù)庫 來儲(chǔ)存消息
private LinkedList list = new LinkedList();
//調(diào)用添加和顯示方法
public void sendMsg(String msg)
{
list.addFirst(msg);
//最多保留10條聊天記錄
if(list.size()>10){
list.removeLast();
}
wctx = WebContextFactory.get();
Util utilThis = new Util(wctx.getScriptSession());
//使用utilThis重置 Id 屬性為 msg 的文本框的內(nèi)容
utilThis.setValue("msg", "請(qǐng)輸入信息");
String currentPage = "/login/showMsg.jsp"; //要推信息的頁面地址
//獲得所有已經(jīng)打開此頁面的會(huì)話
Collection sessions = wctx.getScriptSessionsByPage(currentPage);
Util utilAll = new Util(sessions);
//將消息從LinkedList中取出來,放放到一個(gè)字符串?dāng)?shù)組中
//在這里遇到一個(gè)問題,就是直接用String[] msgs = (String[])list.toArray();時(shí),報(bào)類型轉(zhuǎn)換錯(cuò)誤
//但如下,給toArray()方法傳遞一個(gè)空的數(shù)組就不報(bào)錯(cuò)了,求解中

String[] msgs = new String[list.size()];
msgs = (String[]) list.toArray(msgs);
//先清空頁面的消息內(nèi)容,去除ul元素下所有元素
utilAll.removeAllOptions("ul");
//向頁面添加消息內(nèi)容
utilAll.addOptions("ul", msgs);
}
}
sendMsg.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>DWR反向Ajax示例</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/interface/SendMsg.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/engine.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/util.js'></script>
<script type="text/javascript">
function sendMessage()
{
var msg = $("msg").value;
SendMsg.sendMsg(msg);
}
</script>
</head>
<body>
DWR反向Ajax示例信息添加<br>
輸入信息:<input type="text" id="msg" name="msg" onkeypress="dwr.util.onReturn(event,sendMessage)">
</body>
</html>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>DWR反向Ajax示例</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/interface/SendMsg.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/engine.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/util.js'></script>
<script type="text/javascript">
function sendMessage()
{
var msg = $("msg").value;
SendMsg.sendMsg(msg);
}
</script>
</head>
<body>
DWR反向Ajax示例信息添加<br>
輸入信息:<input type="text" id="msg" name="msg" onkeypress="dwr.util.onReturn(event,sendMessage)">
</body>
</html>
showMsg.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>DWR反向Ajax示例</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/engine.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/util.js'></script>
</head>
<body onload="dwr.engine.setActiveReverseAjax(true);">
DWR反向Ajax示例信息顯示<br>
ul:
<ul id="ul">
</ul>
</body>
</html>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>DWR反向Ajax示例</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/engine.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/util.js'></script>
</head>
<body onload="dwr.engine.setActiveReverseAjax(true);">
DWR反向Ajax示例信息顯示<br>
ul:
<ul id="ul">
</ul>
</body>
</html>
運(yùn)行效果如圖:
這時(shí)showMsg.jsp一直處于失去焦點(diǎn)狀態(tài),卻能實(shí)時(shí)地顯示服務(wù)器端更新的數(shù)據(jù)