DWR入门

来源:互联网 发布:网络违法犯罪举报app 编辑:程序博客网 时间:2024/06/07 13:02

摘自:http://directwebremoting.org/dwr/introduction/getting-started.html

Getting Started with DWR

There are several ways to get started withDWR. We recommend the following:

 

Follow the 5 steps outlined below.

Download the dwr.war file and experimentwith the examples.

Once DWR is up and running learn how towrite JavaScript for DWR.

 

DWR in 5 steps

1. Install the DWR JAR file

Download the dwr.jar file. Place it intothe WEB-INF/lib directory of your web application.

 

2. Install the Commons Logging JAR file

DWR depends on Commons Logging. Downloadthe commons-logging.jar and place it into the WEB-INF/lib directory of your webapplication.

 

3. Add the DWR servlet definition andmapping to your application's web.xml

Add the following lines to your webapplication's deployment descriptor (WEB-INF/web.xml). The <servlet>section needs to go with any existing <servlet> sections, and likewisewith the <servlet-mapping> section.

 

<servlet>

 <display-name>DWR Servlet</display-name>

 <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>

4. Create the DWR configuration file(dwr.xml)

Create a new file in you web application'sWEB-INF directory (alongside web.xml) named dwr.xml.

 

A simple way to start is with somethinglike this:

 

<!DOCTYPE dwr PUBLIC

   "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"

   "http://getahead.org/dwr/dwr30.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 classesDWR can create and remote for use by Javascript. In the example above we aredefining 2 classes that are remoted and giving the classes names in Javascript.

 

The new creator that we used above uses thepublic no-args constructor that all Java Beans must have. It is also worthremembering that DWR has one restriction:

 

Avoid reserved JavaScript words; Methodsnamed after reserved words are automatically excluded. Most JavaScript reservedwords 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 in JavaScript but not Java.

Visit the dwr.xml documentation for adetailed description of the dwr.xml file, creators and converters.

 

5. Take DWR for a test drive!

Go to the following URL:

 

http://localhost:8080/[YOUR-WEBAPP-CONTEXT]/dwr/

You should see a page showing you theclasses you configured in Step 4. Having followed a link you should see anindex of all the methods ready for calling - simply enter the requiredparameters (if parameters are required) and click the execute button. Thesepages are valuable debugging tools and dynamically generated by DWR when thedebug init-param is set to true.

 

How to make use of this from your webapplication

We have a whole section on writingJavascript with DWR, and there are a number of examples in the sidebar thatdemonstrate how to dynamically alter the text in web pages, update lists,manipulate forms and perform live table editing. Each has a description of howit works.

 

Another way to get started is to look atthe source from the pages that you just viewed:

 

Go tohttp://localhost:8080/[YOUR-WEBAPP-CONTEXT]/dwr/ and click on your class

View source and find the line that executesthe method that you are interested in.

Paste the text into an HTML or JSP page inyour web-app.

Include links to the DWR JavaScript filesthat make the magic happen:

<scriptsrc='/[YOUR-WEBAPP-CONTEXT]/dwr/interface/[YOUR-SCRIPT].js'></script>

<scriptsrc='/[YOUR-WEBAPP-CONTEXT]/dwr/engine.js'></script>

You can omit the /[YOUR-WEBAPP-CONTEXT]/section and use relative paths in your web pages if you wish.

 

Next Step: Learn about writing Javascriptthat interacts with DWR.

 

What if it doesn't work?

We have a page containing troubleshootingadvice and common problems and their fixes. This is the first place to look.