DWR – Easy Ajax for Java Ivanka Todorova April 4, 2007
These slides are created from contents from DWR web site http://getahead.org/dwr
Many slides are based on:
Sang Shin. Introduction to DWR (Direct Web Remoting). Retrieved April 1, 2007 on the World Wide Web: http://www.javapassion.com/ajax/DWR.pdf
What is DWR?
Steps for building DWR-based AJAX application
Registering callback functions
Additional Resources with Coding Examples of DWR Applications
What is DWR?
What is DWR?
Hides low-level XMLHttpRequest Handling
Specifically designed with Java technology in mind
“ Easy Ajax for Java”
That’s why it is called “Direct remoting”
Without DWR, you would have to create many Web application endpoints (servlets) that need to be addressable via URIs
If you have several methods in a class on the server that you want to invoke from the browser
Each of these methods need to be addressable via URI
You would have to map parameters and return values to HTML input form parameters and responses yourself
1. How Does DWR Work?
You provide the callback function as additional parameter
2. How DWR Works
DWR Consists of Two Main Parts
A Java Servlet running on the server that processes requests and sends responses back to the browser.
This servlet delegates the call to the backend class you specify in the dwr.xml configuration file
DWR handles XMLHttpRequest handling
Steps for Building DWR-based AJAX Application
Download the dwr.jar file and place it in the WEB-INF/lib directory of your webapp
dwr.jar contains DWR runtime code including the DWR servlet
Edit web.xml in the WEB-INF directory
add servlet and servlet mapping information
Create dwr.xml file in the WEB-INF directory
The scope attribute is optional. It defaults to “page”. The other options are “application”, “session”, and “request”.
The param attribute is used by the various creators for specific bits of configuration. For example the 'new' creator needs to be told what type of object to call 'new' on.
The include and exclude elements allow a creator to restrict access to class methods. A Creator should specify EITHER a list of include elements (which implies that the default policy is denial) OR a list of exclude elements (which implies that the default policy is to allow access).
For example to deny access to all methods in some class except for the setHourlyPay() method you should put the following into your dwr.xml
The new creator is declared by default by DWR as follows: <creator id="new" class="uk.ltd.getahead.dwr.create.NewCreator"/>. It creates an instance of a class using the default constructor.
You allow DWR to use the new creator to create and remote your beans as follows:
Creators and Converters Summary
Creators create objects that live on the server and have their methods remoted
Converters convert parameters and return types
Created objects do things while converted objects carry data
var r=Remote.method (param, callback)
1. Utility Functions
You can even use it outside of DWR because it does not depend on DWR to function
Some of the available utility functions are:
getValue, getValues, setValue, setValues
addRows and removeAllRows
2. Utility Functions
$(id) is the same as
Document.getElementById(id) in DOM API
DWRUtil.getValue(id) get the value(s) out of the HTML elements
DWRUtil.setValue(id, value) finds the element with the id in the first parameter and alters its contents to the second parameter
1. Tutorials and Articles about DWR
DWR Hands-on Lab by Sang Shin, Sun Microsystems. Retrieved April 3, 2007 from the World Wide Web: http://www.javapassion.com/handsonlabs/ajaxdwrintro/
Contains full code for four applications: Chat, Populating Selection List, Form Editing, and Table Editing.
AJAX made simple with DWR by Cloves Carneiro Jr., JavaWorld.com, 06/20/05. Retrieved April 3, 2007 from the World Wide Web: http://www.javaworld.com/javaworld/jw-06-2005/jw-0620-dwr.html
The example application used in this article simulates an apartment rental search engine for the city of Toronto. The user can select a set of search criteria before performing the search.
2. Tutorials and Articles about DWR
Ajax for Java developers: Ajax with Direct Web Remoting by Philip McCarthy. Retrieved April 3, 2007 from the World Wide Web: http://www-128.ibm.com/developerworks/java/library/j-ajax3/
Includes implementation of a shopping cart
Extensive list of tutorials and articles about DWR . Retrieved April 3, 2007 from the World Wide Web: http:// getahead.org/dwr/elsewhere Definitely worth visiting!