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中将方法公布出来
- <!-- 测试,调用方法 -->
- <create javascript="testdwr" creator="new">
- <param name="class" value="com.yinbo.umpay.test.TestDwr" />
- <include method="test1" />
- <include method="test2" />
- <include method="test3" />
- <include method="test4" />
- <include method="test5" />
- <include method="test6" />
- <include method="test7" />
- <include method="test8" />
- <include method="test9" />
- </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
-
- <servlet>
- <servlet-name>dwr-invoker</servlet-name>
- <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
- <init-param>
- <param-name>debug</param-name>
- <param-value>true</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>dwr-invoker</servlet-name>
- <url-pattern>/dwr/*</url-pattern>
- </servlet-mapping>
dwr.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.ltd.uk/dwr/dwr20.dtd">
-
- <dwr>
- <allow>
-
- <!-- umpay dwr methods -->
- <create javascript="dwrFactory" creator="spring">
- <param name="beanName" value="DwrFactoryImpl" />
- <include method="testMethod" />
- <include method="getRoleList" />
- <include method="checkDetealPwd"/>
- <include method="sendSelectBank"/>
- <include method="checkBankaccountId"/>
- <include method="checkBankinterfaceId"/>
- <include method="getAccountList"/>
- <include method="checkUsername"/>
- <include method="checkwwwsite"/>
- <include method="checkAppIllegal"/>
- </create>
-
- <!-- javabean返回值及参数转换 -->
- <convert converter="bean"
- match="com.yinbo.umpay.core.po.Roleinfo">
- <param name="include" value="roleid,rolename" />
- </convert>
-
-
-
- <!-- 测试,调用方法 -->
- <create javascript="testdwr" creator="new">
- <param name="class" value="com.yinbo.umpay.test.TestDwr" />
- <include method="test1" />
- <include method="test2" />
- <include method="test3" />
- <include method="test4" />
- <include method="test5" />
- <include method="test6" />
- <include method="test7" />
- <include method="test8" />
- <include method="test9" />
- </create>
-
- <!-- javabean返回值及参数转换 -->
- <convert converter="bean" match="com.yinbo.umpay.core.po.Userinfo">
- <param name="include" value="userid,username" />
- </convert>
-
- <!-- spring生成 -->
- <create javascript="testdwr2" creator="spring">
- <param name="beanName" value="TestDwrImpl" />
- <include method="test2" />
- </create>
- </allow>
- <!-- List,Set,Map作参数时,声明包含的确切类 -->
- <signatures>
- <![CDATA[
- import java.util.List;
- import com.yinbo.umpay.test.TestDwr;
- import com.yinbo.umpay.core.po.Userinfo;
- TestDwr.test7(List<Userinfo>);
- TestDwr.test9(Map<String, Userinfo>);
- ]]>
- </signatures>
-
- </dwr>
TestDwr.java
- package com.yinbo.umpay.test;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import com.yinbo.umpay.core.po.*;
-
- public class TestDwr {
-
- public void test1() {
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- System.out.println("dwr test1.");
- }
-
- public String test2() {
- return "dwr test2.";
- }
-
- public String test3(String data, String data2) {
- System.out.println(data);
- System.out.println(data2);
- return data + " return " + data2;
-
- }
-
- public Userinfo test4() {
- Userinfo u = new Userinfo();
- u.setUserid("sec.peng");
- u.setUsername("peng yuan feng");
- return u;
- }
-
- public void test5(Userinfo userinfo) {
- System.out.println(userinfo.getUserid());
- System.out.println(userinfo.getUsername());
- }
-
- public List test6() {
- Userinfo u1 = new Userinfo();
- Userinfo u2 = new Userinfo();
- u1.setUserid("sec.peng");
- u1.setUsername("peng yuan feng");
- u2.setUserid("www");
- u2.setUsername("www.163.com");
- List list = new ArrayList();
- list.add(u1);
- list.add(u2);
- return list;
- }
-
- public void test7(List list) {
- for(int i=0; i<list.size(); i++) {
- Userinfo userinfo = (Userinfo)list.get(i);
- System.out.println(userinfo.getUserid());
- System.out.println(userinfo.getUsername());
- }
- }
-
- public Map test8() {
- Map map = new HashMap();
- Userinfo u = new Userinfo();
- u.setUserid("aaa");
- u.setUsername("bbbbbb");
- map.put("u1",u);
- return map;
- }
-
- public void test9(Map map) {
- Userinfo userinfo = (Userinfo)map.get("u1");
- System.out.println(userinfo.getUserid());
- System.out.println(userinfo.getUsername());
- }
-
-
- }
testdwr.jsp
- <%@ page language="java" pageEncoding="UTF-8"%>
-
- <html>
- <head>
- <title>test dwr</title>
- <script src='<c:url value="/dwr/interface/testdwr.js"/>'></script>
- <script src='<c:url value="/dwr/interface/testdwr2.js" />'></script>
- <script src='<c:url value="/dwr/interface/dwrFactory.js"/>' ></script>
- <script src='<c:url value="/dwr/engine.js"/>' ></script>
- <script src='<c:url value="/dwr/util.js"/>' ></script>
- <script>
-
- function callTestMethod1(){
- testdwr.test1();
- }
-
-
- function callTestMethod2(){
- testdwr.test2(callBackFortestMethod2);
- }
- function callBackFortestMethod2(data){
- // 其中 date 接收方法的返回值
- // 可以在这里对返回值进行处理和显示等等
- alert("the return value is " + data);
- }
-
- function callTestMethod3(){
- // 定义要传到 java 方法中的参数
- var data;
- // 构造参数
- data = "test String";
- testdwr.test3(data, "bbb", callBackTestMethod3);
- }
- function callBackTestMethod3(data){
- alert(data);
- }
-
- function callTestMethod4(){
- testdwr.test4(callBackFortestMethod4);
- }
- function callBackFortestMethod4(data){
- // 其中 data 接收方法的返回值
- // 对于 JavaBean 返回值,有两种方式处理
- // 不知道属性名称时,使用如下方法
- /*
- for(var property in data){
- alert("property:"+property);
- alert(property+":"+data[property]);
- }
- */
- // 知道属性名称时,使用如下方法
- alert(data.userid);
- alert(data.username);
- }
-
- function callTestMethod5(){
- // 定义要传到 java 方法中的参数
- var data;
- // 构造参数, date 实际上是一个 object
- data = { userid: "ppp", username:"pengyf" }
- testdwr.test5(data);
- }
-
- function callTestMethod6(){
- testdwr.test6(callBackFortestMethod6);
- }
- function callBackFortestMethod6(data){
- // 其中 date 接收方法的返回值
-
- DWRUtil.addRows('addRowsBasic', data, [
- function(data) { return data.userid; },
- function(data) { return data.username; }
- ]);
-
- if (data != null && typeof data == 'object') alert(dwr.util.toDescriptiveString(data, 2));
- else dwr.util.setValue('d5', dwr.util.toDescriptiveString(data, 1));
-
- // 知道属性名称时,使用如下方法
- for(var i=0;i<data.length;i++){
- alert(data[i].userid);
- alert(data[i].username);
- }
-
-
-
- }
-
-
- function callTestMethod7(){
- // 定义要传到 java 方法中的参数
- var data;
- // 构造参数, date 实际上是一个 object 数组,即数组的每个元素均为 object
- data = [
- {
- userid:"u1",
- username:"user1"
- },
- {
- userid:"u2",
- username:"user2"
- }
- ];
- testdwr.test7(data);
- }
-
-
- function callTestMethod8(){
- testdwr.test8(callBackFortestMethod8);
- }
- function callBackFortestMethod8(data){
- // 其中 date 接收方法的返回值
-
- if (data != null && typeof data == 'object') alert(dwr.util.toDescriptiveString(data, 2));
- else dwr.util.setValue('d5', dwr.util.toDescriptiveString(data, 1));
-
- // 知道属性名称时,使用如下方法
- for(var property in data){
- var bean = data[property];
- alert(bean.userid);
- alert(bean.username);
- }
- }
-
-
- function callTestMethod9(){
- // 定义要传到 java 方法中的参数
- var data;
- // 构造参数, date 实际上是一个 object 数组,即数组的每个元素均为 object
- data = {
- "u1":{
- userid:"uu1",
- username:"user1"
- },
- "u2":{
- userid:"uu2",
- username:"user2"
- }
- };
- testdwr.test9(data);
- }
-
-
- function addOptions(){
- //将数组添加到下拉菜单里面去;
- var arrayFive = [ 'One', 'Two', 'Three', 'Four', 'Five' ];
- DWRUtil.addOptions('addOptionsBasic', arrayFive);
- }
-
- function addOptions2(){
- var arrayObject = [
- { name:'One', value:'1' },
- { name:'Two', value:'2' },
- { name:'Three', value:'3' },
- { name:'Four', value:'4' },
- { name:'Five', value:'5' }
- ];
- //后面2个参数是 值,文本
- DWRUtil.addOptions('addOptionsObject1', arrayObject, "value","name");
-
- //这样调用表示值和文本都是name
- //DWRUtil.addOptions('addOptionsObject1', arrayObject, "name");
-
- }
-
- function addOptions3() {
-
- var map = { one:1, two:2, three:3, four:4, five:5 };
- //同上, one 是值;1 是文本;
- //DWRUtil.addOptions('addOptionsMap1', map);
- //同上, 1 是值;one 是文本;
- DWRUtil.addOptions('addOptionsMap1', map,true);
-
- }
-
- function addOptions4() {
-
- var map = { one:1, two:2, three:3, four:4, five:5 };
- //同上, one 是值;1 是文本;
- //DWRUtil.addOptions('addOptionsMap1', map);
- //同上, 1 是值;one 是文本;
- DWRUtil.addOptions('removeItems', map,true);
-
- }
- function init() {
- dwr.util.useLoadingMessage("加载中...");
- }
-
- function testrows() {
-
- var data = [
- { userid:'One', username:'1'},
- { userid:'tow', username:'2'}
- ];
-
- DWRUtil.addRows('addRowsBasic', data, [
- function(data) { return data.userid; },
- function(data) { return data.username; }
- ],
- {
- rowCreator:function(options) {
- var row = document.createElement("tr");
- var index = options.rowIndex * 50;
- row.style.color = "rgb(" + index + ",0,0)";
- return row;
- },
- cellCreator:function(options) {
- var td = document.createElement("td");
- var index = 255 - (options.rowIndex * 50);
- td.style.backgroundColor = "rgb(" + index + ",255,255)";
- td.style.fontWeight = "bold";
- return td;
- }
- }
- );//end addrows
- }//end function
-
- //callTestMethod1();
-
- function testspring(){
- testdwr2.test2(callBacktestspring);
- }
- function callBacktestspring(data){
- // 其中 date 接收方法的返回值
- // 可以在这里对返回值进行处理和显示等等
- alert("the return value is " + data);
- }
-
-
- function testDwrFactory(){
- dwrFactory.testMethod(callBackTestDwrFactory);
- }
- function callBackTestDwrFactory(data){
- alert("the return value is " + data);
- }
-
-
- </script>
- </head>
- <body onload="init();">
- test dwr
- <br>
- <input type="button" onclick="callTestMethod1();"
- value="调用没有返回值和参数的JAVA方法">
- <br>
-
- <input type="button" onclick="callTestMethod2();"
- value="调用有简单返回值的java方法">
- <br>
-
- <input type="button" onclick="callTestMethod3();"
- value="调用有简单参数的java方法 ">
- <br>
-
- <input type="button" onclick="callTestMethod4();"
- value="调用返回JavaBean的java方法 ">
- <br>
-
- <input type="button" onclick="callTestMethod5();"
- value="调用有JavaBean参数的java方法 ">
- <br>
-
- <input type="button" onclick="callTestMethod6();"
- value="调用返回List、Set或者Map的java方法 ">
- <br>
-
- <input type="button" onclick="callTestMethod7();"
- value="调用有List、Set或者Map参数的java方法 ">
- <br>
-
- <input type="button" onclick="callTestMethod8();"
- value="调用返回Map的java方法 ">
- <br>
-
- <input type="button" onclick="callTestMethod9();"
- value="参数为Map的java方法 ">
- <br>
-
- <p>
- test dwrutil
- <br>
-
- *选中selectRangeBasic文本框里面从第五个字符到第15个字符之间的字符<br>
- <input type="text" id="selectRangeBasic">
- <input type="button" onclick="DWRUtil.selectRange('selectRangeBasic', 5, 15);"
- value="selectRange ">
- <input type="button" onclick="alert(DWRUtil.getSelection('selectRangeBasic')) ;"
- value="_getSelection ">
-
- <br>
-
- *将数组添加到下拉菜单里面去<br>
- <select name="addOptionsBasic">
- </select>
- <input type="button" onclick="addOptions();"
- value="addOptions ">
- <br>
- *得到 addOptionsBasic 对象的值<br>
- <input type="button" onclick="alert(DWRUtil.getValue('addOptionsBasic')) ;"
- value="getValue ">
-
- <br>*得到下拉框 addOptionsBasic 显示的文本<br>
- <input type="button" onclick="alert(DWRUtil.getText('addOptionsBasic')) ;"
- value="getText ">
-
- <br>*将数组及值添加到下拉菜单里面去<br>
- <select name="addOptionsObject1">
- </select>
- <input type="button" onclick="addOptions2();"
- value="addOptions2 ">
- <input type="button" onclick="alert(DWRUtil.getText('addOptionsObject1')) ;"
- value="getText ">
- <input type="button" onclick="alert(DWRUtil.getValue('addOptionsObject1')) ;"
- value="getValue ">
-
- <br>*将对象属性及值添加到下拉菜单里面去<br>
- <select name="addOptionsMap1">
- </select>
- <input type="button" onclick="addOptions3();"
- value="addOptions3 ">
- <input type="button" onclick="alert(DWRUtil.getText('addOptionsMap1')) ;"
- value="getText ">
- <input type="button" onclick="alert(DWRUtil.getValue('addOptionsMap1')) ;"
- value="getValue ">
-
- <br>
- <input type="button" onclick="testrows();"
- value="testrows ">
-
- <input type="button" onclick="DWRUtil.removeAllRows('addRowsBasic');"
- value="removeAllRows ">
-
-
- <br>
-
- <table width="300" border="1" height="30">
- <tbody id="addRowsBasic">
- <tr>
- <th>userid</th>
- <th>username</th>
- </tr>
- </tbody>
- </table>
-
- <input type="button" onclick="testspring();" value="test spring">
-
- <p/>
- <input type="button" onclick="testDwrFactory();" value="umpay dwr">
-
-
- </body>
- </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
雨飞 阅读(131)
评论(0) 编辑 收藏