论坛首页 入门技术论坛

ajax框架之小试牛刀

浏览 2273 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-05-30  
DWR
1,下载DWR.jar包
2,新建web应用,修改web.xml,添加以下内容:
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/dwr.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>scriptCompressed</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

3,在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>
<create creator="new" javascript="Hello">
<param name="class" value="com.bs.dwr.Hello" />

</create>


</allow>
</dwr>

4,写java代码com.bs.dwr.Hello.java:
package com.bs.dwr;

public class Hello {
public String Hello(String name){
return "hello,dwr.--"+name;
}
public String getHello(String name){
return "getHello,dwr.--"+name;
}

}

5,客户端调用,添加testDWR.jsp页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>第一个DWR程序</title>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/Hello.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>

</head>
<body>
<input id="user" type="text" />
<input type='button' value='test' onclick='hello();' />
<input type='button' value='test2' onclick='hello2();' />
<div id="result"></div>
</body>
</html>

<script type="text/javascript">

function hello() { 

var user = document.getElementById('user').value;
  
Hello.Hello(user,callback);

}
function callback(msg) {
   DWRUtil.setValue('result', msg);
   //alert(msg);
}
function hello2(){
var user = document.getElementById('user').value;
Hello.getHello(user,callback);
}

</script>


把整个项目发布到tomcat下,即便可体验到js直接调用java方法来修正页面的快感。
整个项目代码在附件中。



  • testDWR.rar (347.9 KB)
  • 描述: testDWR工程项目完整代码
  • 下载次数: 72
   发表时间:2008-08-28  
一个入门的好例子
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics