浏览 2272 次
锁定老帖子 主题:ajax框架之小试牛刀
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-05-30
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方法来修正页面的快感。 整个项目代码在附件中。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-08-28
一个入门的好例子
|
|
返回顶楼 | |