What is DWR?

Source: Internet
Author: User
Tags object serialization
DWR is an open-source solution using the Apache license protocol. It contains the server-side Java library, a DWR servlet, and a javascript library. although DWR is not the only available Ajax-RPC toolkit on the Java platform, it is the most mature and provides many useful functions.
From the simplest point of view, DWR is an engine that can expose methods of server-side Java objects to JavaScript code. using DWR can effectively eliminate all Ajax request-response loops from application code. this means that the client Code no longer needs to directly process the XMLHTTPRequest object or server response. you no longer need to compile the object serialization code or use a third-party tool to convert the object into XML. you do not even need to write servlet code to adjust Ajax requests to call Java domain objects.
DWR is deployed as a servlet in a web application. think of it as a black box. This servlet has two main functions: first, for each public class, DWR dynamically generates JavaScript contained in the web page. the generated JavaScript contains the stub function, representing the corresponding methods in the Java class and executing XMLHttpRequest behind the scenes. these requests are sent to DWR. At this time, its second role is to translate the requests into method calls on the server-side Java object and put the return values of the methods in the servlet response and send them back to the client, encode it into JavaScript. DWR also provides JavaScript tool functions to help you execute common user interface tasks.
 
Util. js util. js contains some methods to help you use J avascript (possibly) to update your web data from the server.
You can use it outside of DWR because it does not depend on DWR.
It contains four page processing functions: getvalue [s] () and setvalue [s] () Act on most HTML elements except tables, lists, and images. gettext () Act on select lists.
Addrows () and removeallrows () are used to edit tables. addoptions () and removealloptions (). They are used to edit lists (such as select lists, UL, and OL ).
$ ()
$ Function (in J avascript, his name is valid) is introduced from prototype. generally, $ = document. getelementbyid. when you spend a lot of time on AJAX programming in the future, it is helpful to use this format in a proper place.
'$' Finds an element on the current HTML page using the given ID. If more than one parameter is submitted, it returns an array containing the found element. this function is inspired by prototype libraries and can work better in different browsers.
 
Generating lists
A feature of DWR can be used to add options to a select list. You only need to use dwrutil. addoptions ().
If you want to retain some options before updating the list, you need to write the following code:
VaR sel = dwrutil. getvalue (ID );
Dwrutil. removealloptions (ID );
Dwrutil. addoptions (ID ,...);
Dwrutil. setvalue (ID, Sel );
If you want to have an initialization option, such as "Please select", you can directly use:
Dwrutil. addoptions (ID, ["Please select"]);
Dwrutil. addoptions has five calling methods: array: dwrutil. addoptions (selectid, array). selectid is the target ID, and array is the text of each item.
Array of objects (option text = option value): dwrutil. addoptions (selectid, Data, Prop) uses the set of text and value to create an option for each array element. The Pro parameter specifies the value of text and value.
Array of objects (with differing option text and value): dwrutil. addoptions (selectid, array, valueprop, textprop) use the set of text and value to create an option for each array element. valueprop determines the value and textprop determines the text.
Object: dwrutil. addoptions (selectid, MAP, reverse) creates an option for each attribute in map. The attribute name is used as the value of the option, and the attribute value is used as the text of the option. this seems wrong, but in fact it is true. if the reverse parameter is set to true, the attribute value is used as the option value.
Map of objects: dwrutil. addoptions (selectid, MAP, valueprop, textprop) creates an option for each object in the map, valueprop specifies the value of the option, textprop specifies the text of the option.
 
Generating tables
Dwrutil. addrows () retrieves values from an array (the second parameter) and creates each row of the table. the value is obtained from another array (the third parameter) and several columns are created for each row of the table.
 
Dwrutil. gettext (ID)
You can obtain the text value based on the ID. This method can only be used for select list
Dwrutil. getvalue (ID)
You can obtain value by ID. Using this method, you do not need to care about the difference between Div and select list.
Dwrutil. getvalues ()
Getvalues () is similar to getvalue () does t that the input is a JavaScript Object that contains name/value pairs. the names are assumed to be the IDs of HTML elements, and the values are altered to reflect the contents of those IDs. this method does not return the object in question, it alters the value that you pass to it.
This method is the same as getvalue (), but it imports a j avascript object containing the name and value. the name is the ID of the HTML element. this method does not return any objects. It only maps the value of ID to the passed value. example:
Function dogetvalues (){
VaR text = "{
Div: NULL,
Textarea: NULL,
Select: NULL,
Text: NULL,
Password: NULL,
Formbutton: NULL,
Button: NULL
}";
VaR object = objecteval (text); // J avasval object
Dwrutil. getvalues (object );
VaR reply = dwrutil. todescriptivestring (object, 2); // tostring
Reply = reply. Replace (/n/g, "<br/>"); // meaning
Dwrutil. setvalue ("getvaluesret", reply); // display
}
 
Dwrutil. onreturn
Post a piece of code. I don't understand it yet. What is the difference between onreturn and no
<SCRIPT>
Function submitfunction ()
{
$ ("Alert"). style. Display = "inline ";
SetTimeout ("unsubmitfunction ();", 1000 );
}
Function unsubmitfunction ()
{
$ ("Alert"). style. Display = "NONE ";
}
</SCRIPT>
<P> <input type = "text" onkeydown = "dwrutil. onreturn (event, submitfunction)"/>
<Input type = "button" onclick = "submitfunction ()" value = "go"/>
<Span id = "alert" style = "display: none; Background: # ffffdd; font-weight: bold;"> submitfunction called </span>
</P>
 
Dwrutil. selectrange
Select a range in an input box
Dwrutil. selectrange ("sel-test", $ ("START"). value, $ ("end"). value );
 
Dwrutil. setvalue (ID, value)
Use Id to locate the element and update the value.
Dwrutil. setvalues ()
Like setvalue (ID, value), it only requires a j avascript object, for example:
Dwrutil. setvalues ({
Div: "New Div content ",
Password: "1234567890"
});
 
Dwrutil. todescriptivestring
Tostring with debug information. The first parameter is the object to be debugged, and the second parameter is the processing level. The level is as follows:
0: single line of DEBUG single-Line Debugging
1: multi-line debug that does not dig into child objects does not analyze multi-Line Debugging of child elements
2: multi-line debug that digs into the 2nd layer of child objects multi-Line Debugging can be analyzed to the second layer of child elements at most
And so on. Level 2 and greater probably produce too much output.
Conclusion: DWR not only shields a lot of repetitive and complex code between clients and servers, but also provides some common methods. Some ideas are inherited from prototype, and make some improvements. at the same time, it also takes into account the combination of struts, Hibernate and spring.
Note that, DWR is a method that calls the server-side Java code directly from the browser through J avasloud (DWR is a way of calling Java code on the server directly from Javascript in the browser .), instead of a j avascript Library (generally speaking DWR is not a generic JavaScript library so it does not attempt to provide fill this need. however this is one of these really useful functions to have around if you are doing Ajax work .) how much can be done is already very rare.
The version 1.1 Beta 3 (November) proposed by DWR in the draft in has been updated and released more than 20 times, DWR is always visible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.