Posted on 2008-07-17 15:17
G_G 閱讀(1716)
評論(0) 編輯 收藏 所屬分類:
javascript 、
json 、
javaGeneral
參考引用:
使用說明: lib添加dwr.jar
web.xml添加
<?xml?version="1.0"?encoding="ISO-8859-1"?>
<!DOCTYPE?web-app?PUBLIC
????"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN"
????"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app?id="dwr">
??<display-name>DWR?(Direct?Web?Remoting)</display-name>
??<description>A?Simple?Demo?DWR</description>
??<servlet>
????<servlet-name>dwr-invoker</servlet-name>
????<display-name>DWR?Servlet</display-name>
????<description>Direct?Web?Remoter?Servlet</description>
????<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
????<!--?This?should?NEVER?be?present?in?live?-->
????<init-param>
??????<param-name>debug</param-name>
??????<param-value>true</param-value>
????</init-param>
??</servlet>
? <!-- 服務起來后 在地址中直接輸入 http://.../dwr 就可以查看對外提供的服務類 -->
??<servlet-mapping>
????<servlet-name>dwr-invoker</servlet-name>
????<url-pattern>/dwr/*</url-pattern>
??</servlet-mapping>
</web-app>
dwr.xml
<?xml?version="1.0"?encoding="UTF-8"?>
<!DOCTYPE?dwr?PUBLIC
????"-//GetAhead?Limited//DTD?Direct?Web?Remoting?1.0//EN"
????"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
????<allow>
????????<!--?遠程調用類?定義 updateItem,getItem?和?findItems?方法可用??-->
????????<create?creator="new"?javascript="CatalogDAO">
????????????<param?name="class"?value="com.ajaxlab.ajax.CatalogDAO"?/>
????????????<include?method="getItem"?/>
????????????<include?method="findItems"?/>
??????????? <include?method="updateItem"?/>
????????</create>
????????
????????<!--?數據展現定義格式?(javascriot?json?格式)?
????????????此定義?id?name?description?為可見?
??????????????????price?隱藏?
????????????E.g?->?
????????????????{
??????????????????description:"中國制造.",?
??????????????????id:"產品-",?
??????????????????name:"新品-"
????????????????}
????????-->
????????<convert?converter="bean"?match="com.ajaxlab.ajax.Item">
????????????<param?name="include"
????????????????value="id,name,description,formatted-?Price"?/>
????????</convert>
????</allow>
</dwr>
類說明
bean Item:
? private String id = "";
? private String name = "";
? private String description = "";
? private int price = 0;
? get();set();
dao
????public?Item?getItem(String?id)?{
????????Item?item?=?new?Item("產品-"+id);
????????item.setName("新品-"+id);
????????item.setPrice(100);
????????item.setDescription("中國制造.");
????????return?item;
????}
????public?List?findItems(String?expression)?{
????????List?list?=?new?ArrayList();
????????Item?item1?=?new?Item("產品-001");
????????item1.setName("新品-001");
????????item1.setDescription(expression);
????????item1.setPrice(10);
????????Item?item2?=?new?Item("產品-002");
????????item2.setName("新品-002");
????????item2.setDescription(expression);
????????item2.setPrice(15);
????????Item?item3?=?new?Item("產品-003");
????????item3.setName("新品-003");
????????item3.setDescription(expression);
????????item3.setPrice(35);
????????list.add(item1);
????????list.add(item2);
????????list.add(item3);
????????return?list;
????}
?? /* html input text ->
??????? {
??? ??? ??? ? description:"中國制造.",
??? ??? ??? ? id:"1",
??? ??? ??? ? name:"劉凱毅"
??? ??? }
??? ??? return true ;
??? ???
???? */
??? public boolean updateItem(Item item ){
??? ??? if( item!=null && item.getName().equals("劉凱毅") )
??? ??? ??? return true;
??? ???
??? ??? return false ;
??? }
js 方法:
在 input 輸入->>
method( !! )
bean(id,name...)
{id:1,name:'liukaiyi'}
當多參數 為 map
{dd:'liu',aa:'gg'}
js多參數
class.method(
??? objectEval($("p00").value),
??? objectEval($("p01").value),
reply0);展現頁:
<html>
<head>
??<title>DWR?Test</title>
??<!--?These?paths?use?..?so?that?they?still?work?behind?a?path?mapping?proxy.?The?fully?qualified?version?is?more?cut?and?paste?friendly.?-->
??<script?type='text/javascript'?src='/testDwr/dwr/interface/CatalogDAO.js'></script>
??<script?type='text/javascript'?src='/testDwr/dwr/engine.js'></script>
??<script?type='text/javascript'?src='/testDwr/dwr/util.js'></script>
??
??<script?type='text/javascript'>
? //輸入到方法中 參數 格式轉換
??function?objectEval(text){
????text?=?text.replace(/\n/g,?'?');
????text?=?text.replace(/\r/g,?'?');
????if?(text.match(/^\s*\{.*\}\s*$/))
????{
??????text?=?'['?+?text?+?'][0]';
????}
????return?eval(text);
??}
?
? //本例 alert 展現
??var?reply?=?function(data){
??????alert(dwr.util.toDescriptiveString(data,?2));
??}
??</script>
</head>
<body?>
<li>
??findItems(????<input??type='text'??value='""'?id='p00'?/>??);
??<input?class='ibutton'?type='button'?onclick='CatalogDAO.findItems(objectEval($("p00").value),?reply);'?value='Execute'??/>
</li>
<li>
??getItem(????<input?class='itext'?type='text'?size='10'?value='""'?id='p10'?title='Will?be?converted?to:?java.lang.String'/>??);
??<input?class='ibutton'?type='button'?onclick='CatalogDAO.getItem(objectEval($("p10").value),?reply);'?value='Execute'??title='Calls?CatalogDAO.getItem().?View?source?for?details.'/>
</li>
<li>
? updateItem(???<input?class='itext'?type='text'?size='10'?value='{}'?id='p20'/>? );
? <input class='ibutton' type='button' onclick='CatalogDAO.updateItem(objectEval($("p20").value), reply);' value='Execute' />
</li>
</body></html>