浏览 2677 次
锁定老帖子 主题:DWR入门学习
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-04-21
最后修改:2009-04-23
dwr是一种ajax(Asynchronous JavaScript and Xml)的java实现,它提供了一种能在web浏览器端采用js直接调用后台业务组件的解决方案,推存下载(http://directwebremotion.org/) 1. 下载dwr.jar,将其放在web工程的lib文件夹下,并修改web.xml <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> 2. 新建一个java文件,名为TestHello.java package dwr; public class TestHello { public String hello(String input) { return "您输入的字符串为 " + input; } } 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="TestHello"> <param name="class" value="dwr.TestHello" /> </create> </allow> </dwr> 4. 启动eclipse中的tomcat服务器,在ie或内嵌浏览器中输入url(http://127.0.0.1:8080/TestDevon/dwr),显示如下 Classes known to DWR:
TestHello (dwr.TestHello) 5. 点击TestHello, 显示如下
Methods For: TestHello (dwr.TestHello)
To use this class in your javascript you will need the following script includes: <script type='text/javascript' src='/TestDevon/dwr/interface/TestHello.js'></script> <script type='text/javascript' src='/TestDevon/dwr/engine.js'></script> In addition there is an optional utility script: <script type='text/javascript' src='/TestDevon/dwr/util.js'></script> Replies from DWR are shown with a yellow background if they are simple or in an alert box otherwise. The inputs are evaluated as Javascript so strings must be quoted before execution. There are 10 declared methods: hello( ) (button) 6. 新建一个jsp文件,如下 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Test DWR</title> <script type="text/javascript" src="dwr/interface/TestHello.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 reply = function(data) { if (data != null && typeof data == 'object') { alert(dwr.util.toDescriptiveString(data, 2)); } else { dwr.util.setValue('result', dwr.util.toDescriptiveString(data, 1)); } } function setValue() { var value = $("user").value; if (value != "") { TestHello.hello(value, reply); } else { alert("请输入参数!"); } } </script> </head> <body> <input id="user" type="text" /> <input type="button" value="hello" onclick="setValue();" /> <div id="result"></div> </body> </html> 7. 新建一个servlet定向到这个jsp文件,然后运行该servlet,在文本框中输入“张文平”,点击hello铵钮,结果如下 "您输入的字符串为 张文平"
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |