DWR 入门介绍-Ajax

来源:互联网 发布:淘宝上怎么看微淘 编辑:程序博客网 时间:2024/05/18 02:06

Getting Started with DWR 
DWR 入门介绍

There are 2 ways to get started with DWR, the easy way is to download the WAR file and have

a look around, however this does not help you see how easily DWR integrates with your

current web application, so the following 3 simple steps are recommended:
有两种方法可以开始DWR学习,一种就是下载war文件观看,然而这不能帮助你如何将dwr整合到你的web

应用,另外的一种方法就是跟着以下三个简单的步骤去处理:

1. Install the DWR JAR file
1. 安装dwr jar 文件

Download the dwr.jar file. Place it in the WEB-INF/lib directory of your webapp. You'll

probably have a set of jar files in there already.
下载这个dwr.jar 文件 ,将其放在 web-inf/lib/文件夹里面。你或者已经有很多的jar在这个文件夹

里面。

2. Edit the config files
修改config文件

The following lines need to be added to WEB-INF/web.xml. The <servlet> section needs to go

with the other <servlet> sections, and likewise with the <servlet-mapping> section.
将下面的代码添加到WEB-INF/web.xml文件中  这个<servlet>段需要与其他<servlet>段在一起,

servlet-mapping 也是这样处理
<servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <display-name>DWR Servlet</display-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>

 

Then create a dwr.xml file that lives in WEB-INF alongside web.xml. A simple way to start

is with something like this:
在web-inf 里面 创建 一个dwr.xml文件。 一个简单的方法就是用以下的代码:
<!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="JDate">
      <param name="class" value="java.util.Date"/>
    </create>
    <create creator="new" javascript="Demo">
      <param name="class" value="your.java.Bean"/>
    </create>
  </allow>
</dwr>

The DWR config file defines what classes DWR can create and remote for use by Javascript.

In the example above we are defining 2 classes that are remoted and giving the classes

names in Javascript.
dwr的配置文件中定义哪个类需要创建及javascript远程调用.在上述的列子中我们是定义了两个类给

javascript远程调用.

The new creator that we used above uses the public no-args constructor that all JavaBeans

must have. It is also worth remembering that DWR has a few restrictions:
我们上面所用的新创建器必须调用没参数的构造函数,从而dwr有一些规则必须要记住:

Avoid reserved JavaScript words; Methods named after reserved words are automatically

excluded. Most JavaScript reserved words are also Java reserved words, so you won't be

having a method called "try()" anyway. However the most common gotcha is "delete()", which

has special meaning from JavaScript but not Java.
避免使用javascript保留词,方法名是会在保留词之后自动排除. 大多数的javascript保留词其实也是

java的保留词,所以你将不用使用一个叫try的方法.

Overloaded methods can be involved in a bit of a lottery as to which gets called, so avoid

overloaded methods.
重载方法在javascript中是不是那么正常调用,所以避免使用重载方法.

3. Go to the following URL
http://localhost:8080/[YOUR-WEBAPP]/dwr/
You should see a page showing you the classes that you've selected in step 2. Having

followed a link you should see an index of all the methods all ready for calling. These

pages are dynamically generated examples of what you can do using DWR.
3.打入以下连接http://localhost:8080/[YOUR-WEBAPP]/dwr/
你将见到一个页面向你展示所有第二步你所选择的类.点击每个类的连接,你将看见所有可以准备调用的

方法.这些页面是动态生成的你使用dwr的例子.