Ajax框架 DWR 简介(1)---DWR基本设置
2006-04-20 15:07
351 查看
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:
Then create a dwr.xml file that lives in WEB-INF alongside web.xml. A simple way to start is with something like this:
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
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.
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.
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:
You can omit the
For more information about how to write Javascript that interacts with DWR see the scripting introduction.
1. Install the DWR JAR file
Download the dwr.jar file. Place it in theWEB-INF/libdirectory 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 toWEB-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" ""> <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
newcreator 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.
相关文章推荐
- Ajax框架 DWR 简介(3)---配置web.xml文件
- Java简介及基本设置
- pin 的使用简介 ——环境设置 基本工具的使用 相关知识
- Ajax框架 DWR 简介(2)---Examples
- 网站服务器基本安全设置
- 一.javaweb笔记之javaScript简介+基本语法+事件驱动+DOM
- ubuntu10.10 防火墙 ufw 设置简介
- 第一次打开Photoshop时的基本设置
- 关于saltstack简介,安装和基本配置
- Spark基本概念简介
- SVG基本形状及样式设置
- SVG基本形状及样式设置
- 网络编程的基本概念,TCP/IP协议简介
- ubuntu14.04设置基本工作环境:搭建c 开发环境,搜狗输入法,触摸板
- 第1章Cucumber简介与环境设置
- UIDatePicker 基本设置-显示已经选择的日期 并设置最大、最小日期
- CentOS 6.5 web服务器apache的安装与基本设置
- swiper基本用法之设置分页器样式(二)