util.js
? util.js包含了一些使用的方法,從而幫助你利用javascript(可能)從服務器端更新你的web數據。
? 你可以在DWR之外的地方使用它,因為它并不依賴與DWR而實現。
? 它包含四個頁面處理函數:getValue[s]()、setValue[s]()作用于除tables、lists和images以外的大多數html元素。getText()作用于select lists。
? addRows()和removeAllRows()用于編輯tables。addOptions()和removeAllOptions()用于編輯lists(如:select lists、ul、ol)。
$()
? $函數(在javascript中,他的名字是合法的)的思想是從prototype引進的。一般說來,$ = document.getElementById。在以后你花大量時間進行ajax編程的時候,在合適的地方使用這種格式是很有益的。
? '$'通過給定的ID在當前HTML頁面找到元素,如果多于一個的參數被提交,它就會返回一個包含已找到元素的數組。這個函數從prototype的library中得到的靈感,而且,它還能更好的工作在不同的瀏覽器中。
?
Generating Lists
? DWR的一個功能可以給一個下拉列表(select list)添加選項,只需使用DWRUtil.addOptions()。
? 如果你在更新列表之前,希望保留一些選項,你需要寫以下一些代碼:
????? var sel = DWRUtil.getValue(id);
????? DWRUtil.removeAllOptions(id);
????? DWRUtil.addOptions(id, ...);
????? DWRUtil.setValue(id, sel);
? 如果你想有個初始化選項,如:“please select”,你可以直接使用:
????? DWRUtil.addOptions(id, ["Please select"]);
DWRUtil.addOptions 有5種調用方法:
Array: DWRUtil.addOptions(selectid, array) 。selectid為目標ID,array為每一項的text。
Array of objects (option text = option value): DWRUtil.addOptions(selectid, data, prop) 用text和value的集合來為每一個數組元素創建一個選項,pro參數指定text和value的值。
Array of objects (with differing option text and value): DWRUtil.addOptions(selectid, array, valueprop, textprop) 用text和value的集合來為每一個數組元素創建一個選項,valueprop確定value,textprop確定text。
Object: DWRUtil.addOptions(selectid, map, reverse) 為map中每一個屬性(property)創建一個選項,屬性名作為選項的value,屬性的value作為選項的text。這樣做看起來是錯的,但實際上這種做法的確是正確的。如果reverse參數被設置為true,則屬性的value還是被用做選項的value。
Map of objects: DWRUtil.addOptions(selectid, map, valueprop, textprop) 為map中的每一個對象創建一個選項,valueprop指定選項的value,textprop指定選項的text。
?
Generating Tables
DWRUtil.addRows() 從一個數組(第二個參數)取得值,創建table的每一行。從另一個數組(第三個參數)去得值,為table的每一行創建若干列。
?
DWRUtil.getText(id)
可以根據id取得text的值,這個方法只能用于select list
DWRUtil.getValue(id)
可以根據id取得value,使用這個方法,你不必在意div和select list的不同。
DWRUtil.getValues()
getValues() is similar to getValue() except that the input is a Javascript object that contains name/value pairs. The names are assumed to be the IDs of HTML elements, and the values are altered to reflect the contents of those IDs. This method does not return the object in question, it alters the value that you pass to it.
這個方法和getValue()一樣,只是它傳入的是一個包含名字和數值的javascript對象.這個名字就是HTML元素的ID。這個方法不會返回任何對象,它只會將ID的value映射給傳入的value。例:
function doGetValues() {
? var text= "{
? div:null,
? textarea:null,
? select:null,
? text:null,
? password:null,
? formbutton:null,
? button:null
}";
? var object = objectEval(text);??? //javascript對象
? DWRUtil.getValues(object);
? var reply = DWRUtil.toDescriptiveString(object, 2);?? //toString
? reply = reply.replace(/\n/g, "<br/>");??????????????????????? //轉意
? DWRUtil.setValue("getvaluesret", reply);???????????????????? //顯示
}
?
DWRUtil.onReturn
貼一段代碼,暫時不理解,用onReturn和不用有什么區別
<script>
function submitFunction()
{
??? $("alert").style.display = "inline";
??? setTimeout("unsubmitFunction();", 1000);
}
function unsubmitFunction()
{
??? $("alert").style.display = "none";
}
</script>
<p><input type="text" onkeydown="DWRUtil.onReturn(event, submitFunction)"/>
<input type="button" onclick="submitFunction()" value="GO"/>
<span id="alert" style="display:none; background:#FFFFDD; font-weight:bold;">submitFunction called</span>
</p>
?
DWRUtil.selectRange
在一個input box里選一個范圍
DWRUtil.selectRange("sel-test", $("start").value, $("end").value);
?
DWRUtil.setValue(id, value)
用ID找到元素,并更新value
DWRUtil.setValues()
和setValue(id,value)一樣,只是它需要的參數是個javascript對象,如:
DWRUtil.setValues({
? div: "new div content",
? password: "1234567890"
});
?
DWRUtil.toDescriptiveString
帶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 最多分析到第二層子元素的多行調試
And so on. Level 2 and greater probably produce too much output.
?
總結:DWR不但屏蔽了許多client與server交互的重復且復雜的代碼,而且還提供了一些常用的方法,一些思想還是從prototype繼承而來,并有一定的改進。同時,它也考慮到了與struts、hibernate、spring的結合問題。
需要注意的是,DWR是一種把服務器端的java代碼通過javascript直接從瀏覽器調用的方法(DWR isa way of calling Java code on the server directly from Javascript in the browser.),而不是一個javascript的庫(Generally speaking DWR is not a generic JavaScript library so it does not attempt to provide fill this need. However this is one of these really useful functions to have around if you are doing Ajax work.)能做到怎么多,已經很難得了。
DWR自04年11月草案提出到現在的Version 1.1 beta 3(2005-12-29),已經更新發布了20多次了,但愿在ajax的發展大路上,能始終看見DWR的身影。
原文章出自:http://www.3qblog.com/oblog312/user1/E_wsq/archives/2006/1366.html