在网上找了很多dwr的例子。不是很齐全,补充如下:
需要的jar是dwr.jar。180kb的。不是最新版本。
dwr.xml和web.xml是同级目录
1.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="dwr/interface/MyJsNameForDwr.js"></script>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript">
//var callBackValue = function callBack(data)
function callBackValue(data)
{
DWRUtil.setValue("text", data);
}
function callBackValue2(data)
{
DWRUtil.setValue("objectvalues", data);
}
function getJavaMethodsValues()
{
MyJsNameForDwr.getSomething('a', callBackValue);
var objectValue = {
aString:'x',
bInt:'2',
objectV:{list:'list',string:'string'}
};
MyJsNameForDwr.getSomethingObject(objectValue, callBackValue2);
}
function clearValue()
{
document.getElementById('text').value = "";
}
</script>
</head>
<body>
<h1>
Ajax _DWR
</h1>
<input type="button" name="show" value="调用java方法" onclick="getJavaMethodsValues();">
<input type="button" name="clear" value="清空值" onclick="javascript:clearValue();">
<br>
<input type="text" name="text" id="text">
<input type="text" name="objectvalues" id="objectvalues">
</body>
</html>
2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.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>
<convert match="com.dwr.ObjectV" converter="bean">
<param name="list,string"></param>
</convert>
<convert match="com.dwr.ObjectValues" converter="bean">
<param name="aString,bInt,objectV"></param>
</convert>
<create creator="new" javascript="MyJsNameForDwr" scope="application">
<param name="class" value="com.dwr.Class" />
<include method="getSomething" />
<include method="getSomethingObject" />
</create>
</allow>
</dwr>
4.Class.java
package com.dwr;
public class Class
{
/**
* 从前台传递参数到后台方法调用
* @param par
* @return
*/
public boolean getSomething(String par)
{
System.out.println("par == " + par);
String par2 = "b";
System.out.println("par2 == " + par2);
if (par.equals("a")&&par2.equals("b"))
{
return true;
}
else
{
return false;
}
}
/**
* 从前台传递对象到后台
* @param
* @return
*/
public boolean getSomethingObject(ObjectValues obj)
{
System.out.println("aString = " + obj.getaString());
System.out.println("bInt = " + obj.getbInt());
System.out.println("ObjectV = " + obj.getObjectV().getString());
if (obj.getaString().equals("x"))
{
return true;
}
else
{
return false;
}
}
}
5.ObjectValues.java
package com.dwr;
public class ObjectValues
{
/**
* 前台传递到后台的对象包装
*/
private String aString;
private int bInt;
private ObjectV objectV;
public String getaString()
{
return aString;
}
public void setaString(String aString)
{
this.aString = aString;
}
public int getbInt()
{
return bInt;
}
public void setbInt(int bInt)
{
this.bInt = bInt;
}
public ObjectV getObjectV()
{
return objectV;
}
public void setObjectV(ObjectV objectV)
{
this.objectV = objectV;
}
}
6.ObjectV.java
package com.dwr;
import java.util.List;
public class ObjectV
{
private String list;
private String string;
public String getList()
{
return list;
}
public void setList(String list)
{
this.list = list;
}
public String getString()
{
return string;
}
public void setString(String string)
{
this.string = string;
}
}