专栏首页源哥的专栏Getting Started with DWR

Getting Started with DWR

Getting Started with 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:

1. Install the DWR JAR file

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.

2. Edit the config files

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.

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

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

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:

  • 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.
  • Overloaded methods can be involved in a bit of a lottery as to which gets called, so avoid overloaded methods.

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.

Kick the tyres and have a look around.

How to make use of this from your web application

There are a number of examples in the sidebar that demonstrate how to dynamically alter the text in web pages, update lists, manipulate forms and do live table editing. Each has a description of how it works.

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

  • Go to http://localhost:8080/[YOUR-WEBAPP]/dwr/ and click on you class
  • View source and find the line that executes the method that you are interested in.
  • Paste the text into an HTML or JSP page in your web-app.
  • Include links to the javascript files that make the magic happen:
<script src='/[YOUR-WEBAPP]/dwr/interface/[YOUR-SCRIPT].js'></script>
<script src='/[YOUR-WEBAPP]/dwr/engine.js'></script>

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

For more information about how to write Javascript that interacts with DWR see the scripting introduction.

What if it doesn't work?

We have a log of common problems and their fixes. This is the first place to look.

If you are still having problems please join the mailing list and ask there.

Posted by kevinwu on December 19, 2006 11:44 AM | 全文 |AJAX|del.icio.us|VIVI|365key|blogChina|Poco|SOHU|Hexun

<script src="城市胡同 Getting Started with DWR_files/show_ads.js" type="text/javascript"> </script> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-8908527459010687&amp;dt=1175787751906&amp;lmt=1173626868&amp;prev_fmts=728x90_as&amp;format=728x15_0ads_al&amp;output=html&amp;url=file%3A%2F%2F%2FH%3A%2FnewBooks%2F%E7%BD%91%E7%AB%99%E4%B8%8B%E8%BD%BD%2Fdwr%2F%E5%9F%8E%E5%B8%82%E8%83%A1%E5%90%8C%2520Getting%2520Started%2520with%2520DWR.htm&amp;cc=100&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameborder="0" width="728" scrolling="no" height="15" allowtransparency="allowtransparency"></iframe>

相关文档_

Ajax开发框架+用户名检测实例(调试通过) - Jul 03, 2006

Ajax按需读取数据生成下级菜单[转] - Jul 03, 2006

Ajax在PHP开发中的应用[转] - Jun 27, 2006

利用PHP和AJAX创建RSS聚合器 - May 10, 2006

Ajax文档 - May 01, 2006

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Getting started with React Native

    If you are amongst those who have come across the buzz of React Native and are h...

    用户4822892
  • Docker Getting started with Java

    Docker 官网提供了 python,nodejs,java 3种不同编程语言的 Language-specific guides 学习指南。该指南详细说明了...

    Se7en258
  • Getting started with Kotlin on Android

    摘要 Kotlin是一门与Swift类似的静态类型JVM语言,由JetBrain设计开发并开源,与Java相比,Kotlin的语法更简洁,更具表达性,而且提供了...

    IT大咖说
  • Getting Started with PubNub on IBM Bluemix

    image.png PubNub is a secure global Data Stream Network (DSN) and easy-to-use A...

    首席架构师智库
  • vueRouter-Getting Started

    用Vue.js+vue-router创建单页应用是非常简单的。使用Vue.js,我们已经可以通过组合组件来组成应用程序,当你把vue-router添加进来,我们...

    tianyawhl
  • Android Training Summary (1) Getting Started

    Android Training 中Getting Started部分的阅读笔记

    宅男潇涧
  • 【竞赛相关】Kaggle活跃竞赛的最新汇总

    双十一将至,为了方便大家顺利完成的竞赛。我们整理了现有Kaggle平台上的比赛信息,加油奥利给!

    黄博的机器学习圈子
  • 翻译qmake文档(二) Getting Started

    原英文文档: http://qt-project.org/doc/qt-5/qmake-tutorial.html       本教程教讲授qmake基础知识。...

    lpxxn
  • 强化学习仿真环境搭建入门Getting Started with OpenAI gym

    gym是用于开发和比较强化学习算法的工具包。它不对代理的结构做任何假设,并且与任何数字计算库(例如TensorFlow或Theano)兼容。

    列夫托尔斯昊

扫码关注云+社区

领取腾讯云代金券