1,请先参阅–DWR中文文档 v0.9 — For Dwr 2.0–这是最新版的DWR中文教程,下载DWR相关.jar包和.js文件下载地址: http://directwebremoting.org/dwr/download 找到

Download Version 2.0.5 Size
JAR File: dwr.jar(下载) (490Kb)
WAR File: dwr.war (2.14Mb)
Sources: dwr-2.0.5-src.zip (下载) (20.77Mb)

2,将dwr.jar放到: 你的工程/WEB-INF/lib/下

   用解压缩软件打开-src.zip文件,进入目录:\/java\org\directwebremoting,找到engine.js和 util.js并将其解压出来,在 你的工程/WebRoot/ 下新建文件夹 dwr\interface,将engine.js和util.js放在dwr\下

   以下两步为必要的准备工作,如果你要在html中测试dwr的话

3,打开WEB-INF\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>
</web-app>

      其中黑体加粗部分为DWR2.x版的DwrServlet位置,如果用DWR1.x,应改为:
          <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
      开发者应该参考dwr.jar文件中的实际位置写

4,写一个简单的类文件:

package collon;

public class Service {
    public String sayHello(String yourName){
   return “Welcome :” + yourName;
    }
}

5,在WEB-INF\下新建dwr.xml,内容为下:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE dwr PUBLIC “-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN”            “http://www.getahead.ltd.uk/dwr/dwr20.dtd“>
<dwr>
<allow>
   <create creator=”new” javascript=”service”>
<param name=”class” value=”collon.Service”/>
   <include method=”sayHello”/>
   </create>
</allow>
</dwr>

     注意黑体加粗字,是dwr2.0的标准DTD文档声明,其中,javascript=”service”为要生成的,可远程调用的,javascript方法名

6,启动tomcat服务器吧

7,打开网页,输入http://localhost:8080/你的工程/dwr   按回车,测试吧,会有如下输出:


    Classes known to DWR:
service (collon.Service)

点service,会进入如下页面:


Methods For: service (collon.Service)

To use this class in your javascript you will need the following script includes:

<script type=\'text/javascript\' src=\'/firstDwr/dwr/interface/service.js\'></script>
<script type=\'text/javascript\' src=\'/firstDwr/dwr/engine.js\'></script>

In addition there is an optional utility script:

<script type=\'text/javascript\' src=\'/firstDwr/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:


sayHello( ); <script type=”text/javascript”></script> (在双引号中输入字符串,可以测试了)……………

8,点击 \’/firstDwr/dwr/interface/service.js\’ 将其保荐到 \WebRoot\dwr\interface 下

9,新建html页面,代码如下:

<%@ page language=”java” import=”java.util.*” pageEncoding=”gbk”%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
    <title>My Dwr Testing</title>
    <script type=\’text/javascript\’ src=\’/你的工程/dwr/interface/service.js\’></script>
    <script type=\’text/javascript\’ src=\’/你的工程/dwr/engine.js\’></script>
    <script type=\’text/javascript\’ src=\’/你的工程/dwr/util.js\’></script>
    <script type=\’text/javascript\’ src=\’/你的工程/js/first.js\’></script>
</head>
<body>
   
<input type=”text” width=”100″ id=”yourName” /><input
type=”button” value=”Click Me” onclick=”demo();” /><br />
    <div id=”result”></div>
</body>
</html>

注:请注意各.js文件的位置要准确无误

10:

建立/你的工程/js/first.js 文件,内容如下:

function demo(){
    service.sayHello($(\’yourName\’).value,callback);//注意,service.sayHello是js文件代码,不是Java类Service.sayHello
    }
function callback(msg){
   DWRUtil.setValue(“result”,msg);
}

11,输入页面测试吧

12,请我们看一看DwrServlet生成的.js代码:

// Provide a default path to dwr.engine
if (dwr == null) var dwr = {};
if (dwr.engine == null) dwr.engine = {};
if (DWREngine == null) var DWREngine = dwr.engine;

if (service == null) var service = {};
service._path = \’/firstDwr/dwr\’;
service.sayHello = function(p0, callback) {
dwr.engine._execute(service._path, \’service\’, \’sayHello\’, p0, callback);
}

不知道大家看出点什么没有

好了,就写到就里了,有时间再研究一下 DWR 的 源代码

版权声明:本文为dafengshou原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/dafengshou/p/3655658.html