DWR introduction and usage

Publisher:冰雪勇士Latest update time:2011-06-11 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

introduce

It consists of two main parts: allowing JavaScript to obtain data from a Servlet on a WEB server that follows the AJAX principle. On the other hand, a JavaScript library can help website developers easily use the obtained data to dynamically change the content of the web page.

DWR takes a new approach similar to AJAX to dynamically generate JavaScript code based on JAVA classes. In this way, WEB developers can use Java code in JavaScript as if they were local code (client-side code) of the browser; but Java code runs on the WEB server and can freely access the resources of the WEB server. For security reasons, WEB developers must properly configure which Java classes can be safely used externally.
This Java-to-JavaScript approach to remote functionality gives DWR users an interface that looks very much like traditional RPC mechanisms, like RMI or SOAP, but with the benefit of running on the web without the need for browser plugins.
DWR does not consider browser/web server protocols important, but is more interested in ensuring a simple and natural programming interface. The biggest challenge is to combine the asynchronous nature of AJAX with the synchronous nature of normal JAVA method calls. In asynchronous mode, the result data can only be obtained by asynchronous access some time after the call is started. DWR allows web developers to pass a callback function to asynchronously handle the Java function call process.
In addition, .dwr is also a file format: For example, in Dreamweaver software, there is a search and replace function, and the frequently used search and replace content can be saved, and the saved file ends with .dwr. This kind of file can be opened with Notepad to see its content.

How to use

1.1、Configuration of dwr.xml
The tag contains things that can be exposed to JavaScript.
The tag specifies the Java class that can be accessed in JavaScript and defines how DWR should obtain an instance of the class to be remoted. The creator="new" attribute specifies how the Java class instance is generated. New means that DWR should call the default constructor of the class to obtain the instance. There are other spring methods, such as obtaining the instance by integrating with the IOC container Spring, etc. The javascript=" testClass " attribute specifies the name used by the JavaScript code to access the object.
The tag specifies the Java class name to be exposed to JavaScript.
The tag specifies the methods to be exposed to JavaScript. If not specified, all methods will be exposed.
The tag specifies methods to prevent access.
1.2、Calling in javascript
First, import the javascript script
Among them, TestClass.js is automatically generated by dwr according to the configuration file, and engine.js and util.js are script files that come with dwr.
Second, write a javascript function that calls the java method
Function callTestMethod1(){
testClass.testMethod1();
}
2. Calling a Java method with a simple return value
2.1、Configuration of dwr.xml
Same configuration as 1.1
2.2、Calling in javascript
First, import the javascript script
Secondly, write a javascript function that calls the java method and a callback function that receives the return value
Function callTestMethod2(){
testClass.testMethod2(callBackFortestMethod2);
}
Function callBackFortestMethod2(data){
//The return value of the data receiving method
//You can process and display the return value here, etc.
alert("the return value is " + data);
}
Where callBackFortestMethod2 is the callback function that receives the return value
3. Calling Java methods with simple parameters
3.1、Configuration of dwr.xml
Same configuration as 1.1
3.2、Calling in javascript
First, import the javascript script
Second, write a javascript function that calls the java method
Function callTestMethod3(){
//Define the parameters to be passed to the java method
var data;
//Construction parameters
data = "test String";
testClass.testMethod3(data);
}
4. Call the Java method that returns the JavaBean
4.1、Configuration of dwr.xml
The tag is responsible for exposing classes and class methods for Web remoting, while the tag is responsible for the parameters and return types of these methods. The function of the convert element is to tell DWR how to convert data types between server-side Java object representation and serialized JavaScript. DWR automatically adjusts simple data types between Java and JavaScript representation. These types include Java native types and their respective encapsulated class representations, as well as String, Date, array, and collection types. DWR can also convert JavaBeans into JavaScript representations, but for security reasons, explicit configuration is required. The tag completes this function. The converter="bean" attribute specifies that the conversion method uses the JavaBean naming specification, the match=""com.dwr.TestBean" attribute specifies the javabean name to be converted, and the tag specifies the JavaBean property to be converted.
4.2、Calling in javascript
First, import the javascript script
Secondly, write a javascript function that calls the java method and a callback function that receives the return value
Function callTestMethod4(){
testClass.testMethod4(callBackFortestMethod4);
}
Function callBackFortestMethod4(data){
//The return value of the data receiving method
//There are two ways to handle the JavaBean return value
//When you don't know the attribute name, use the following method
for(var property in data){
alert("property:"+property);
alert(property+":"+data[property]);
}
//When you know the attribute name, use the following method
alert(data.username);
alert(data.password);
}
Where callBackFortestMethod4 is the callback function that receives the return value
5. Calling Java methods with JavaBean parameters
5.1、Configuration of dwr.xml
Same configuration as 4.1
5.2、Calling in javascript
First, import the javascript script
Second, write a javascript function that calls the java method
Function callTestMethod5(){
//Define the parameters to be passed to the java method
var data;
//Construction parameters, data is actually an object
data = { username:"user", password:"password" }
testClass.testMethod5(data);
}
6. Call a Java method that returns a List, Set, or Map
6.1、Configuration of dwr.xml
Same configuration as 4.1
Note: If the elements in List, Set, or Map are simple types (including their encapsulated classes) or String, Date, array, and collection types, the tag is not required.
6.2、Call in javascript (taking returning List as an example, the element of List is TestBean)
First, import the javascript script
Secondly, write a javascript function that calls the java method and a callback function that receives the return value
Function callTestMethod6(){
testClass.testMethod6(callBackFortestMethod6);
}
Function callBackFortestMethod6(data){
//The return value of the data receiving method
//There are two ways to handle the JavaBean return value
//When you don't know the attribute name, use the following method
for(var i=0;i
for(var property in data[i]){
alert("property:"+property);
alert(property+":"+data[i][property]);
}
}
//When you know the attribute name, use the following method
for(var i=0;i
alert(data[i].username);
alert(data[i].password);
}
}
7. Calling Java methods with List, Set or Map parameters
7.1、Configuration of dwr.xml
import java.util.List;
import com.dwr.TestClass;
import com.dwr.TestBean;
TestClass.testMethod7(List);
]]>
The tag is used to declare the exact class contained in the List, Set, or Map parameters in the Java method so that the Java code can make judgments.
7.2、Call in javascript (taking returning List as an example, the element of List is TestBean)
First, import the javascript script
Second, write a javascript function that calls the java method
Function callTestMethod7(){
//Define the parameters to be passed to the java method
var data;
//Construction parameters, data is actually an object array, that is, each element of the array is an object
data = [
{
username:"user1",
password:"password2"
},
{
username:"user2",
password:" password2"
}
];
testClass.testMethod7(data);
}
Notice:
1. For the sixth case, if the return value of the Java method is Map, the JavaScript callback function that receives the return value is processed as follows:
function callBackFortestMethod(data){
//The return value of the data receiving method
for(var property in data){
var bean = data[property];
alert(bean.username);
alert(bean.password);
}
}
2. For the seventh case, if the parameter of the Java method is Map (assuming its key is String and value is TestBean), use the following method to construct the parameters to be passed in the JavaScript function that calls the method:
function callTestMethod (){
//Define the parameters to be passed to the java method
var data;
//Construct parameters, date is actually an object, its attribute name is the key of Map, and its attribute value is the value of Map
data = {
"key1":{
username:"user1",
password:"password2"
},
"key2":{
username:"user2",
password:" password2"
}
};
testClass.testMethod(data);
}
And add the following configuration section in dwr.xml
import java.util.List;
import com.dwr.TestClass;
import com.dwr.TestBean;
TestClass.testMethod7(Map);
]]>
3. From the above, we can find that when the return value of the Java method is List (Set), DWR converts it into an Object array and passes it to JavaScript; when the return value of the Java method is Map, DWR converts it into an Object, where the properties of the Object are the key values ​​of the original Map, and the property values ​​are the corresponding values ​​of the original Map.
4. If the parameters of the Java method are List (Set) and Map, JavaScript should also construct the corresponding JavaScript data according to the three methods to pass it to Java.
Special Notes:
When creating, you must first instantiate it: var obj = new Object(); obj = {key:"value"}; You can also use obj.key = "value"; If it is not initialized, the conversion will fail.

Installation and Configuration

1. Install the DWR JAR package
Download the dwr.jar file and place it in the WEB-INF/lib directory of your web application. There may already be some jar files in this directory.
2. Edit the config file
Add the following code to the WEB-INF/web.xml file. needs to be placed after another , and the same is true for .
dwr-invoker
DWR Servlet
uk.ltd.getahead.dwr.DWRServlet
debug
true
dwr-invoker
/dwr/*
Next, create a dwr.xml file and place it in the WEB-INF directory where web.xml is located. The file should look something like this:
”-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN”
”http://www.getahead.ltduk/dwr/dwr10.dtd”>
The DWR configuration file defines the classes created by DWR and used remotely by Javascript. In the above example we created 2 classes remotely and gave the class names in Javascript.
The new creator used above uses the public no-args constructor that all JavaBeans must have. It is worth mentioning that DWR has some limitations:
Avoid using JavaScript reserved words; methods named after reserved words are automatically excluded. Most JavaScript reserved words are also Java reserved words, so you can't have a method named "try()" anyway. But the most commonly used word, "delete()", has a special meaning in JavaScript that it doesn't in Java.
Overloaded methods will fall into unknown situations when called, so overloaded methods should be avoided.
3. Visit the following address
http://localhost:8080/[YOUR-WEBAPP]/dwr/
You should see a page showing the class you created in step 2. Clicking on a link will show you a list of all the methods waiting to be called. You can also use DWR to do these dynamically generated examples.
Try it and experience it for yourself.
How to apply it to your web application?
In the sidebar there are many examples showing how to change text on a web page, update lists, manipulate forms, and modify tables dynamically. Each example has a detailed description.
Another way to get started is to view the source code of the page you just viewed:
Go to http://localhost:8080/[YOUR-WEBAPP]/dwr/ and click on the class you created;
View the source code and locate the line of code of the method you are interested in;
Paste this text into an HTML or JSP page of your web application;
Contains the following javascript files:
You can modify the /[YOUR-WEBAPP]/ part according to your actual situation.
Reference address:DWR introduction and usage

Previous article:AJAX Introduction and Technology
Next article:Data model concepts and type classification

Popular Resources
Popular amplifiers
Latest Analog Electronics Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号