什么是DWR?
DWR是一个Open Source的 java项目。DWR可以让JavaScript调用运行在Web服务器里面的JAVA程序。简单一点或者专业一点就是Easy AJAX for JAVA.
下面将一步一步的介绍怎么完成一个简单DEMO
1,从官方网站下载DWR https://dwr.dev.java.net/files/documents/2427/32252/dwr.war 把他直接放到TOMCAT_HOME/webapps 下面
2,编写一个Java类如下:
DWRTest.java
package com.test.ajax;
/**
*
* @author 方见华
*
*/
public class DWRTest {
public DWRTest(){
}
public String getMyName(){
return "James Fang";
}
}
3, 把DRWTest类配置在dwr.xml中,dwr.xml是DWR的配置文件,所有需要在JavaScript中调用的java的类都必须在这个文件中描述 。这个文件的位于TOMCAT_HOME/webapps/dwr/WEB-INF/dwr.xml. 在<allow>标签中添加以下内容
<create creator="new" javascript="MyTest" scope="application">
<param name="class" value="com.test.ajax.DWRTest"/>
</create>
4, 编写调用DWRTest类 getMyName的方法的HTML文件,test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>DWR - My First AJAX</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type='text/javascript' src='/dwr/dwr/interface/MyTest.js'></script>
<script type='text/javascript' src='/dwr/dwr/engine.js'></script>
<script type='text/javascript' src='/dwr/dwr/util.js'></script>
<script language="javascript">
var mycall=function callBack(data){
DWRUtil.setValue("demo1",data);
}
function showMyName(){
MyTest.getMyName(mycall);
}
function clearName(){
demo1.value="";
}
</script>
</head>
<body>
<h1>Ajax Test Page</h1>
<input type="button" value="setMyName" onclick="javascript:showMyName()"><input type="button" value="Clear" onclick="javascript:clearName()"><br>
<input type="text" id="demo1"></select><br>
</body></html>
5, 运行,在浏览器中输入 http://localhost:8080/dwr/test.html
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=669314