posts - 0,  comments - 1,  trackbacks - 0
DWR

DWR现在已经在java平台的AJAX应用中使用比较广泛,下面将以前项目中用到的部分内容(测试部分)贴出来,以供参考。

配置使用什么的就不多说了,在网上搜一下就很多,非常简单。

只简单的说一下流程,然后直接把文件中的内容贴出来。具体的使用也可以参考dwr的网站:http://directwebremoting.org/dwr

1.首先在web.xml中配置dwr,以便在项目中使用

2.编写java应用,并在dwr.xml中配置(我的dwr.xml在web-info下)。

3.在jsp文件中调用

例如:

TestDwr.java中有一个方法 public String test2(); 如果想在jsp页面中通过ajax方式调用步骤如下:

在dwr.xml中将方法公布出来

  1. <!-- 测试,调用方法  -->
  2.         <create javascript="testdwr" creator="new">
  3.             <param name="class" value="com.yinbo.umpay.test.TestDwr" />
  4.             <include method="test1" />
  5.             <include method="test2" />
  6.             <include method="test3" />
  7.             <include method="test4" />
  8.             <include method="test5" />
  9.             <include method="test6" />
  10.             <include method="test7" />
  11.             <include method="test8" />
  12.             <include method="test9" />
  13.         </create>

javascript="testdwr"是指会生成testdwr.js的文件供前台调用。 value="com.yinbo.umpay.test.TestDwr" 是指方法所在的类,creator="new"是构造方法,这里使用new对象的方法,也可以通过spring来管理。这里注意即使用在TestDwr.java中是public方法,也要在配置文件中发布出来才可以访问。

在jsp中调用如下:

先要引入

<script src='<c:url value="/dwr/engine.js"/>' ></script>
  <script src='<c:url value="/dwr/util.js"/>' ></script>

这两个js是支持库,必须引入。然后就是引入

<script src='<c:url value="/dwr/interface/testdwr.js"/>'></script>

这个testdwr.js是dwr动态生成的,你不用去寻找它放在哪儿。

jsp中的调用


function callTestMethod2(){
      testdwr.test2(callBackFortestMethod2);
}

function callBackFortestMethod2(data){
     // 其中 date 接收方法的返回值
     // 可以在这里对返回值进行处理和显示等等
 alert("the return value is " + data);
}

 

  <input type="button" onclick="callTestMethod2();"
   value="调用有简单返回值的java方法">
  

这里的调用使用testdwr这个对象引用方法。testdwr.test2(callBackFortestMethod2);  返回值会传入callBackFortestMethod2方法的参数中。

 

web.xml

  1. <!-- dwr -->
  2.     <servlet>
  3.         <servlet-name>dwr-invoker</servlet-name>
  4.         <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
  5.         <init-param>
  6.             <param-name>debug</param-name>
  7.             <param-value>true</param-value>
  8.         </init-param>
  9.     </servlet>
  10.     <servlet-mapping>
  11.         <servlet-name>dwr-invoker</servlet-name>
  12.         <url-pattern>/dwr/*</url-pattern>
  13.     </servlet-mapping>

 

dwr.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.ltd.uk/dwr/dwr20.dtd">
  3. <dwr>
  4.     <allow>
  5.         <!-- umpay dwr methods -->
  6.         <create javascript="dwrFactory" creator="spring">
  7.             <param name="beanName" value="DwrFactoryImpl" />
  8.             <include method="testMethod" />
  9.             <include method="getRoleList" />
  10.             <include method="checkDetealPwd"/>
  11.             <include method="sendSelectBank"/>
  12.             <include method="checkBankaccountId"/>
  13.             <include method="checkBankinterfaceId"/>
  14.             <include method="getAccountList"/>
  15.             <include method="checkUsername"/>
  16.             <include method="checkwwwsite"/>    
  17.             <include method="checkAppIllegal"/>
  18.         </create>
  19.         <!-- javabean返回值及参数转换 -->
  20.         <convert converter="bean"
  21.             match="com.yinbo.umpay.core.po.Roleinfo">
  22.             <param name="include" value="roleid,rolename" />
  23.         </convert>
  24.         <!-- 测试,调用方法  -->
  25.         <create javascript="testdwr" creator="new">
  26.             <param name="class" value="com.yinbo.umpay.test.TestDwr" />
  27.             <include method="test1" />
  28.             <include method="test2" />
  29.             <include method="test3" />
  30.             <include method="test4" />
  31.             <include method="test5" />
  32.             <include method="test6" />
  33.             <include method="test7" />
  34.             <include method="test8" />
  35.             <include method="test9" />
  36.         </create>
  37.         
  38.         <!-- javabean返回值及参数转换 -->
  39.         <convert converter="bean" match="com.yinbo.umpay.core.po.Userinfo">
  40.             <param name="include" value="userid,username" />
  41.         </convert>
  42.         
  43.         <!-- spring生成   -->
  44.         <create javascript="testdwr2" creator="spring">
  45.             <param name="beanName" value="TestDwrImpl" />
  46.             <include method="test2" />
  47.         </create>
  48.     </allow>
  49.     <!-- List,Set,Map作参数时,声明包含的确切类 -->
  50.     <signatures>
  51.         <![CDATA[ 
  52.             import java.util.List; 
  53.             import com.yinbo.umpay.test.TestDwr;
  54.             import com.yinbo.umpay.core.po.Userinfo; 
  55.             TestDwr.test7(List<Userinfo>); 
  56.             TestDwr.test9(Map<String, Userinfo>); 
  57.         ]]>
  58.     </signatures>
  59. </dwr>

 

TestDwr.java

  1. package com.yinbo.umpay.test;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import com.yinbo.umpay.core.po.*;
  7. public class TestDwr {
  8.     public void test1() {
  9.         try {
  10.             Thread.sleep(5000);
  11.         } catch (InterruptedException e) {
  12.             // TODO Auto-generated catch block
  13.             e.printStackTrace();
  14.         }
  15.         System.out.println("dwr test1.");
  16.     }
  17.     
  18.     public String test2() {
  19.         return "dwr test2.";
  20.     }
  21.     
  22.     public String test3(String data, String data2) {
  23.         System.out.println(data);
  24.         System.out.println(data2);
  25.         return data + " return " + data2;
  26.         
  27.     }
  28.     public Userinfo test4() {
  29.         Userinfo u = new Userinfo();
  30.         u.setUserid("sec.peng");
  31.         u.setUsername("peng yuan feng");
  32.         return u;
  33.     }
  34.     
  35.     public void test5(Userinfo userinfo) {
  36.         System.out.println(userinfo.getUserid());
  37.         System.out.println(userinfo.getUsername());
  38.     }
  39.     
  40.     public List test6() {
  41.         Userinfo u1 = new Userinfo();
  42.         Userinfo u2 = new Userinfo();
  43.         u1.setUserid("sec.peng");
  44.         u1.setUsername("peng yuan feng");
  45.         u2.setUserid("www");
  46.         u2.setUsername("www.163.com");
  47.         List list = new ArrayList();
  48.         list.add(u1);
  49.         list.add(u2);
  50.         return list;
  51.     }
  52.     
  53.     public void test7(List list) {
  54.         for(int i=0; i<list.size(); i++) {
  55.             Userinfo userinfo = (Userinfo)list.get(i);
  56.             System.out.println(userinfo.getUserid());
  57.             System.out.println(userinfo.getUsername());
  58.         }
  59.     }
  60.     
  61.     public Map test8() {
  62.         Map map = new HashMap();
  63.         Userinfo u = new Userinfo();
  64.         u.setUserid("aaa");
  65.         u.setUsername("bbbbbb");
  66.         map.put("u1",u);
  67.         return map;
  68.     }
  69.     
  70.     public void test9(Map map) {
  71.             Userinfo userinfo = (Userinfo)map.get("u1");
  72.             System.out.println(userinfo.getUserid());
  73.             System.out.println(userinfo.getUsername());
  74.     }
  75.     
  76.     
  77. }

 

testdwr.jsp

  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <html>
  3.     <head>
  4.         <title>test dwr</title>
  5.         <script src='<c:url value="/dwr/interface/testdwr.js"/>'></script>
  6.         <script src='<c:url value="/dwr/interface/testdwr2.js" />'></script>
  7.         <script src='<c:url value="/dwr/interface/dwrFactory.js"/>' ></script>
  8.         <script src='<c:url value="/dwr/engine.js"/>' ></script>
  9.         <script src='<c:url value="/dwr/util.js"/>' ></script>
  10.         <script>
  11. function callTestMethod1(){
  12.       testdwr.test1(); 
  13. } 
  14. function callTestMethod2(){ 
  15.       testdwr.test2(callBackFortestMethod2); 
  16. } 
  17. function callBackFortestMethod2(data){ 
  18.      // 其中 date 接收方法的返回值 
  19.      // 可以在这里对返回值进行处理和显示等等 
  20.     alert("the return value is " + data); 
  21. } 
  22. function callTestMethod3(){ 
  23.       // 定义要传到 java 方法中的参数 
  24.       var data; 
  25.       // 构造参数 
  26.       data = "test String"; 
  27.       testdwr.test3(data, "bbb", callBackTestMethod3); 
  28. } 
  29. function callBackTestMethod3(data){ 
  30. alert(data);
  31. }
  32. function callTestMethod4(){ 
  33.       testdwr.test4(callBackFortestMethod4); 
  34. } 
  35. function callBackFortestMethod4(data){ 
  36.      // 其中 data 接收方法的返回值 
  37. // 对于 JavaBean 返回值,有两种方式处理 
  38.              // 不知道属性名称时,使用如下方法 
  39.             /*
  40.            for(var property in data){ 
  41.               alert("property:"+property); 
  42.               alert(property+":"+data[property]); 
  43.            } 
  44.            */
  45. // 知道属性名称时,使用如下方法
  46.            alert(data.userid); 
  47.            alert(data.username); 
  48. } 
  49. function callTestMethod5(){ 
  50.                  // 定义要传到 java 方法中的参数 
  51.       var data; 
  52.       // 构造参数, date 实际上是一个 object 
  53.       data = { userid: "ppp", username:"pengyf" } 
  54.       testdwr.test5(data); 
  55. } 
  56. function callTestMethod6(){ 
  57.       testdwr.test6(callBackFortestMethod6); 
  58. } 
  59. function callBackFortestMethod6(data){ 
  60.      // 其中 date 接收方法的返回值 
  61.         
  62.         DWRUtil.addRows('addRowsBasic', data, [
  63. function(data) { return data.userid; },
  64. function(data) { return data.username; }
  65. ]);
  66.     
  67.                   if (data != null && typeof data == 'object') alert(dwr.util.toDescriptiveString(data, 2));
  68.       else dwr.util.setValue('d5', dwr.util.toDescriptiveString(data, 1));
  69.       
  70.             // 知道属性名称时,使用如下方法 
  71.             for(var i=0;i<data.length;i++){ 
  72.                            alert(data[i].userid); 
  73.                            alert(data[i].username); 
  74.             } 
  75.     
  76.         
  77. }
  78. function callTestMethod7(){ 
  79. // 定义要传到 java 方法中的参数 
  80.       var data; 
  81.       // 构造参数, date 实际上是一个 object 数组,即数组的每个元素均为 object 
  82. data = [ 
  83.                       { 
  84.                          userid:"u1",
  85.                          username:"user1" 
  86.                       }, 
  87.                       { 
  88.                          userid:"u2",
  89.                          username:"user2" 
  90.                       } 
  91.                   ]; 
  92.       testdwr.test7(data); 
  93. } 
  94. function callTestMethod8(){ 
  95.       testdwr.test8(callBackFortestMethod8); 
  96. } 
  97. function callBackFortestMethod8(data){ 
  98.      // 其中 date 接收方法的返回值 
  99.             
  100.                   if (data != null && typeof data == 'object') alert(dwr.util.toDescriptiveString(data, 2));
  101.       else dwr.util.setValue('d5', dwr.util.toDescriptiveString(data, 1));
  102.       
  103.             // 知道属性名称时,使用如下方法 
  104.          for(var property in data){ 
  105.                   var bean = data[property]; 
  106.                   alert(bean.userid); 
  107.                   alert(bean.username); 
  108.               }  
  109. } 
  110. function callTestMethod9(){ 
  111. // 定义要传到 java 方法中的参数 
  112.       var data; 
  113.       // 构造参数, date 实际上是一个 object 数组,即数组的每个元素均为 object 
  114.     data = { 
  115.                          "u1":{ 
  116.                              userid:"uu1",
  117.                              username:"user1" 
  118.                          }, 
  119.                          "u2":{ 
  120.                             userid:"uu2",
  121.                             username:"user2" 
  122.                          } 
  123.                      }; 
  124.       testdwr.test9(data); 
  125. } 
  126. function addOptions(){
  127. //将数组添加到下拉菜单里面去;
  128. var arrayFive = [ 'One', 'Two', 'Three', 'Four', 'Five' ];
  129. DWRUtil.addOptions('addOptionsBasic', arrayFive); 
  130. }
  131. function addOptions2(){
  132. var arrayObject = [
  133. { name:'One', value:'1' },
  134. { name:'Two', value:'2' },
  135. { name:'Three', value:'3' },
  136. { name:'Four', value:'4' },
  137. { name:'Five', value:'5' }
  138. ];
  139. //后面2个参数是 值,文本
  140. DWRUtil.addOptions('addOptionsObject1', arrayObject,  "value","name"); 
  141. //这样调用表示值和文本都是name
  142. //DWRUtil.addOptions('addOptionsObject1', arrayObject, "name");
  143. }
  144. function addOptions3() {
  145. var map = { one:1, two:2, three:3, four:4, five:5 };
  146. //同上, one 是值;1 是文本;
  147. //DWRUtil.addOptions('addOptionsMap1', map); 
  148. //同上, 1 是值;one 是文本;
  149. DWRUtil.addOptions('addOptionsMap1', map,true); 
  150. }
  151. function addOptions4() {
  152. var map = { one:1, two:2, three:3, four:4, five:5 };
  153. //同上, one 是值;1 是文本;
  154. //DWRUtil.addOptions('addOptionsMap1', map); 
  155. //同上, 1 是值;one 是文本;
  156. DWRUtil.addOptions('removeItems', map,true); 
  157. }
  158.  function init() {
  159.     dwr.util.useLoadingMessage("加载中...");
  160.   }
  161.  function testrows() { 
  162.  var data = [
  163. { userid:'One', username:'1'},
  164. { userid:'tow', username:'2'}
  165. ];
  166.         DWRUtil.addRows('addRowsBasic', data, [
  167. function(data) { return data.userid; },
  168. function(data) { return data.username; }
  169. ],
  170. {
  171.     rowCreator:function(options) {
  172.     var row = document.createElement("tr");
  173.     var index = options.rowIndex * 50;
  174.     row.style.color = "rgb(" + index + ",0,0)";
  175.     return row;
  176.     },
  177.     cellCreator:function(options) {
  178.     var td = document.createElement("td");
  179.     var index = 255 - (options.rowIndex * 50);
  180.     td.style.backgroundColor = "rgb(" + index + ",255,255)";
  181.     td.style.fontWeight = "bold";
  182.     return td;
  183.     }
  184. }
  185. );//end addrows
  186. }//end function
  187. //callTestMethod1();
  188. function testspring(){ 
  189.       testdwr2.test2(callBacktestspring); 
  190. } 
  191. function callBacktestspring(data){ 
  192.      // 其中 date 接收方法的返回值 
  193.      // 可以在这里对返回值进行处理和显示等等 
  194.     alert("the return value is " + data); 
  195. } 
  196. function testDwrFactory(){ 
  197.       dwrFactory.testMethod(callBackTestDwrFactory); 
  198. } 
  199. function callBackTestDwrFactory(data){ 
  200.     alert("the return value is " + data); 
  201. } 
  202. </script>
  203.     </head>
  204.     <body  onload="init();">
  205.         test dwr
  206.         <br>
  207.         <input type="button" onclick="callTestMethod1();"
  208.             value="调用没有返回值和参数的JAVA方法">
  209.         <br>
  210.         <input type="button" onclick="callTestMethod2();"
  211.             value="调用有简单返回值的java方法">
  212.         <br>
  213.         <input type="button" onclick="callTestMethod3();"
  214.             value="调用有简单参数的java方法 ">
  215.         <br>
  216.         <input type="button" onclick="callTestMethod4();"
  217.             value="调用返回JavaBean的java方法 ">
  218.         <br>
  219.         <input type="button" onclick="callTestMethod5();"
  220.             value="调用有JavaBean参数的java方法 ">
  221.         <br>
  222.         
  223.         <input type="button" onclick="callTestMethod6();"
  224.             value="调用返回List、Set或者Map的java方法 ">
  225.         <br>
  226.         <input type="button" onclick="callTestMethod7();"
  227.             value="调用有List、Set或者Map参数的java方法  ">
  228.         <br>
  229.         
  230.         <input type="button" onclick="callTestMethod8();"
  231.             value="调用返回Map的java方法  ">
  232.         <br>
  233.         
  234.         <input type="button" onclick="callTestMethod9();"
  235.             value="参数为Map的java方法  ">
  236.         <br>
  237. <p>
  238. test dwrutil
  239. <br>
  240. *选中selectRangeBasic文本框里面从第五个字符到第15个字符之间的字符<br>
  241. <input type="text" id="selectRangeBasic"> 
  242. <input type="button" onclick="DWRUtil.selectRange('selectRangeBasic', 5, 15);"
  243.             value="selectRange  ">
  244. <input type="button" onclick="alert(DWRUtil.getSelection('selectRangeBasic')) ;"
  245.             value="_getSelection  ">
  246.             
  247. <br>
  248. *将数组添加到下拉菜单里面去<br>
  249. <select name="addOptionsBasic">
  250. </select>
  251. <input type="button" onclick="addOptions();"
  252.             value="addOptions  ">           
  253. <br>
  254. *得到 addOptionsBasic 对象的值<br>
  255. <input type="button" onclick="alert(DWRUtil.getValue('addOptionsBasic')) ;"
  256.             value="getValue  "> 
  257. <br>*得到下拉框 addOptionsBasic 显示的文本<br>
  258. <input type="button" onclick="alert(DWRUtil.getText('addOptionsBasic')) ;"
  259.             value="getText  ">  
  260. <br>*将数组及值添加到下拉菜单里面去<br>
  261. <select name="addOptionsObject1">
  262. </select>
  263. <input type="button" onclick="addOptions2();"
  264.             value="addOptions2  ">  
  265. <input type="button" onclick="alert(DWRUtil.getText('addOptionsObject1')) ;"
  266.             value="getText  ">                              
  267. <input type="button" onclick="alert(DWRUtil.getValue('addOptionsObject1')) ;"
  268.             value="getValue  ">
  269.             
  270. <br>*将对象属性及值添加到下拉菜单里面去<br>
  271. <select name="addOptionsMap1">
  272. </select>
  273. <input type="button" onclick="addOptions3();"
  274.             value="addOptions3  ">  
  275. <input type="button" onclick="alert(DWRUtil.getText('addOptionsMap1')) ;"
  276.             value="getText  ">                              
  277. <input type="button" onclick="alert(DWRUtil.getValue('addOptionsMap1')) ;"
  278.             value="getValue  ">                     
  279.                         
  280. <br>
  281. <input type="button" onclick="testrows();"
  282.             value="testrows  ">
  283.             
  284.             <input type="button" onclick="DWRUtil.removeAllRows('addRowsBasic');"
  285.             value="removeAllRows  ">
  286.             
  287.             
  288. <br>
  289.     
  290.        <table width="300" border="1" height="30">
  291.             <tbody id="addRowsBasic">
  292.             <tr>
  293.             <th>userid</th>
  294.             <th>username</th>
  295.             </tr>
  296.             </tbody>
  297.         </table>    
  298.         
  299.         <input type="button" onclick="testspring();" value="test spring">
  300.         
  301.         <p/>
  302.         <input type="button" onclick="testDwrFactory();" value="umpay dwr">
  303.         
  304.             
  305.     </body>
  306. </html>



public class StocksDemo implements Runnable
{

 private ServerContext sctx;
 private ServletContext servletContext;

 public StocksDemo()
    {
       db.connect();
           
        servletContext = WebContextFactory.get().getServletContext();
        sctx = ServerContextFactory.get(servletContext);

    }

public synchronized void toggle()
    {
       active = !active;

        if (active)
        {
            new Thread(this).start();
        }
    }

 public void run()
    {
        try
        {
           
            log.debug(" Starting server-side thread");
            while (active)
            {
                Collection sessions = sctx.getScriptSessionsByPage("/stocksdemo/index.html");
                Util pages = new Util(sessions);
                pages.setValue("requestUpdate", null);

                log.debug("Sent message");
                Thread.sleep(1000);
            }
           
            log.debug(" Stopping server-side thread");
        }
        catch (InterruptedException ex)
        {
            ex.printStackTrace();
        }
    }
posted on 2009-04-15 15:42 雨飞 阅读(146) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2026年11月>
日一二三四五六
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

留言簿

文章档案

搜索

  •  

最新评论