|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册
x
1. 导入dwr.jar包;
2. 配置web.xml
如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- <init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
-->
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
<!-- org.directwebremoting.servlet.DwrServlet 2.X-->
<!--uk.ltd.getahead.dwr.DWRServlet 1.X-->
3. 写服务端代码(写类)
如:
package com.sk.dwr;
public class Test {
//返回一般数据
public String getString(String s){
return s "Hello";
}
//返回对象
Public String getUser(String name){
Return new User(name);
}
}
4. 于web.xml同目录下添加并配置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标签
<allow>:要暴露给js的方法,可以有多个
<create creator="new" javascript="Hello">
<!—new表示调用无参构造new对象,Hello表示new出的对象的句柄,用于在js中调用 -->
<param name="class" value="com.sk.dwr.Test"/>
<!—name表示类,value指定类的完整路径 -->
</create>
[1] [2]
<!-- java方法返回对象时,必须指定对象及其要暴露给js的属性 -->
<convert converter="bean"
match="com.sk.dwr.Person"/>
<!-- ,用于取对象的属性(有几个就写几个)
<param name="include" value="username,password" /> -->
</allow>
</dwr>
5. 写HTML(jsp)页面
如:
<meta http-equiv="Context-Type" content="text/html; charset=UTF-8">
<html >
<head>
<title>TabContainer Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<script type='text/javascript' src="<%=request.getContextPath()%>/dwr/interface/Hello.js"></script>
//Hello.js由dwr生成,于要调用的页面,引用Hello.js
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<!--//**注意:/dwr红色部分必须与web.xml的/dwr部分相同,否则,js报错**-->
<script type="text/javascript">
var person;
function call()
{ //主调函数;
var nameValue=document.getElementById("name").value;
//alert(nameValue);
Hello.getString(nameValue,callback);
//Hello.getUser(nameValue,callback);//参数1,java方法需要的参数,参数2,调用回调函数,dwr自动把返回的数据放入callback的参数内!
//JDate.toString(callback);
}
function callback(User) {
//回调函数;
DWRUtil.setValue("mydiv",User);
//若User为ArrayList,则User.username
//alert(User.password);
}
</script>
</head>
<body>
<table>
<tr>
<td>Name:</td>
<td><input id="name" type="text"/>
<input type="button" value="Read" />
</td>
</tr>
</table>
<div id="mydiv"></div>
</body>
</html> 上一页 [1] [2] |
|