最近朋友托我帮他搞个dwr列子,所以帮他写了个。今天贴出来是为了方便那些还在外面找dwr列子的人看的。
1.定义一个接口。。
package com.whaccp.bean;
public interface IHello
{
public String hello(String name);
}
2。实现接口
package com.whaccp.bean;
public class Hello implements IHello
{
public String hello(String name)
{
return String.format("%1$s : Hello World!!!!", name);
}
}
3.dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "dwr10.dtd" >
<dwr>
<allow>
<create javascript="Hello" creator="new" scope="request">
<param name="class" value="com.whaccp.bean.Hello"></param>
</create>
</allow>
</dwr>
4.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>
</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>
5.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type='text/javascript'
src='/DRWSamplePrj/dwr/interface/Hello.js'></script>
<script type='text/javascript' src='/DRWSamplePrj/dwr/engine.js'></script>
<script type='text/javascript' src='/DRWSamplePrj/dwr/util.js'></script>
<script type="text/javascript">
function hello(){
//name=document.getElementById('txtName').value;
name=$('txtName').value;
Hello.hello(name,function(returnStr){
$('spWord').innerHTML=returnStr;
});
}
</script>
<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">
-->
</head>
<body>
Name:
<input id="txtName" type="text" />
<input type="submit" value="Hello" onclick="hello()"/>
<br/>
<span id="spWord"/>
</body>
</html>
此列子需要dwr.jar包,另外版本需要的是5.5的,6.0的配置变了