Learning about Ajax using DWR

Source: Internet
Author: User

1. Call a Java method without return values or parameters

1.1. configuration of DWR. xml
<DWR> <br/> <allow> <br/> <create creator = "new" javascript = "testclass"> <br/> <Param name = "class" value = "/COM. DWR. testclass "/> <br/> <include method =" testmethod1 "/> <br/> </create> <br/> </allow> <br/> </DWR>
<Allow> the tag contains things that can be exposed to JavaScript access.
<Create> the tag specifies the Java class that can be accessed in JavaScript and defines how DWR gets the instance of the class to be remotely implemented. Creator = "new" attribute specifies the Java class instance generation method. New means that DWR should call the default constructor of the class to obtain the instance. Other methods include the spring method, by integrating with the IOC container spring to obtain instances. The javascript = "testclass" attribute specifies the name used by the JavaScript code to access the object.
<Param> label specifies the Java class name to be exposed to JavaScript.
<Include> the tag specifies the method to be exposed to JavaScript. If this parameter is not specified, all methods are exposed.
<Exclude> label specifies the method to prevent access.
1.2 calls in Javascript
First, introduce JavaScript scripts
<SCRIPT src = 'dwr/interface/testclass. js'> </SCRIPT>
<SCRIPT src = "/DWR/engine. js"> </SCRIPT>
<SCRIPT src = "/DWR/util. js"> </SCRIPT>
Testclass. JS is automatically generated by DWR according to the configuration file, and engine. js and util. js are built-in script files of DWR.
Second, write JavaScript Functions that call Java methods.
Function calltestmethod1 (){
Testclass. testmethod1 ();
}
2. Call a Java method with a simple return value

2.1. configuration of DWR. xml
Configuration is the same as 1.1
<DWR>
<Allow>
<Create creator = "new" javascript = "testclass">
<Param name = "class" value = "/COM. DWR. testclass"/>
<Include method = "testmethod2"/>
</Create>
</Allow>
</DWR>
2.2 calls in Javascript
First, introduce JavaScript scripts
Second, write the JavaScript function that calls the Java method and the callback function that receives the returned value.
Function calltestmethod2 (){
Testclass. testmethod2 (callbackfortestmethod2 );
}
Function callbackfortestmethod2 (data ){
// The Return Value of the date receiving Method
// The returned values can be processed and displayed here.
Alert ("the return value is" + data );
}
Callbackfortestmethod2 is the callback function that receives the returned value.

3. Call the Java method with simple parameters

3.1. configuration of DWR. xml
Configuration is the same as 1.1
<DWR>
<Allow>
<Create creator = "new" javascript = "testclass">
<Param name = "class" value = "/COM. DWR. testclass"/>
<Include method = "testmethod3"/>
</Create>
</Allow>
</DWR>
3.2 calls in Javascript
First, introduce JavaScript scripts
Second, write JavaScript Functions that call Java methods.
Function calltestmethod3 (){
// Define the parameters to be passed to the Java method
VaR data;
// Construct parameters
Data = "test string ";
Testclass. testmethod3 (data );
}

4. Call the Java method that returns JavaBean
4.1. configuration of DWR. xml
<DWR>
<Allow>
<Create creator = "new" javascript = "testclass">
<Param name = "class" value = "/COM. DWR. testclass"/>
<Include method = "testmethod4"/>
</Create>
<Convert converter = "Bean" match = "" com. DWR. testbean ">
<Param name = "include" value = "username, password"/>
</Convert>
</Allow>
</DWR>
<Creator> labels are used to publish methods for remote web classes and classes. <convertor> labels are used to specify the parameters and return types of these methods. The function of the convert element is to tell DWR how to convert the data type between the Java object representation on the server and the serialized JavaScript. DWR automatically adjusts simple data types between Java and JavaScript representations. These types include the Java Native type and their respective encapsulation class representation, as well as the string, date, array, and set types. DWR can also convert Javabean to Javascript, but explicit configuration is required for security reasons. The <convertor> label completes this function. Converter = "Bean" attribute specifies the conversion method using the JavaBean naming convention, match = "" com. DWR. testbean "attribute specifies the name of the JavaBean to be converted, and the <Param & gt; tag specifies the JavaBean attribute to be converted.
4.2 calls in Javascript
First, introduce JavaScript scripts
Second, write the JavaScript function that calls the Java method and the callback function that receives the returned value.
Function calltestmethod4 (){
Testclass. testmethod4 (callbackfortestmethod4 );
}
Function callbackfortestmethod4 (data ){
// The Return Value of the date receiving Method
// There are two ways to process the return value of JavaBean
// If you do not know the attribute name, use the following method:
For (VAR property in data ){
Alert ("Property:" + property );
Alert (property + ":" + data [property]);
}
// Use the following method when you know the property name
Alert (data. username );
Alert (data. Password );
}
Callbackfortestmethod4 is the callback function that receives the returned value.

5. Call the Java method with the JavaBean Parameter
5.1. configuration of DWR. xml
Configuration is the same as 4.1
<DWR>
<Allow>
<Create creator = "new" javascript = "testclass">
<Param name = "class" value = "/COM. DWR. testclass"/>
<Include method = "testmethod5"/>
</Create>
<Convert converter = "Bean" match = "com. DWR. testbean">
<Param name = "include" value = "username, password"/>
</Convert>
</Allow>
</DWR>
5.2 calls in Javascript
First, introduce JavaScript scripts
Second, write JavaScript Functions that call Java methods.
Function calltestmethod5 (){
// Define the parameters to be passed to the Java method
VaR data;
// Construct a parameter. date is actually an object
Data = {Username: "user", password: "password "}
Testclass. testmethod5 (data );
}

6. Call the Java method that returns list, set, or map.
6.1. configuration of DWR. xml
Configuration is the same as 4.1
<DWR>
<Allow>
<Create creator = "new" javascript = "testclass">
<Param name = "class" value = "/COM. DWR. testclass"/>
<Include method = "testmethod6"/>
</Create>
<Convert converter = "Bean" match = "com. DWR. testbean">
<Param name = "include" value = "username, password"/>
</Convert>
</Allow>
</DWR>
Note: The <convert> label is not required if the elements in list, set, or map are of simple type (including its encapsulation class), String, date, array, and set type.
6.2 calls in JavaScript (taking the returned list as an example, the list element is testbean)
First, introduce JavaScript scripts
Second, write the JavaScript function that calls the Java method and the callback function that receives the returned value.
Function calltestmethod6 (){
Testclass. testmethod6 (callbackfortestmethod6 );
}
Function callbackfortestmethod6 (data ){
// The Return Value of the date receiving Method
// There are two ways to process the return value of JavaBean
// If you do not know the attribute name, use the following method:
For (VAR I = 0; I <data. length; I ++ ){
For (VAR property in data ){
Alert ("Property:" + property );
Alert (property + ":" + data [property]);
}
}
// Use the following method when you know the property name
For (VAR I = 0; I <data. length; I ++ ){
Alert (data. username );
Alert (data. Password );
}
}

7. Call Java methods with list, set, or map Parameters

7.1. configuration of DWR. xml
<DWR>
<Allow>
<Create creator = "new" javascript = "testclass">
<Param name = "class" value = "/COM. DWR. testclass"/>
<Include method = "testmethod7"/>
</Create>
<Convert converter = "Bean" match = "com. DWR. testbean">
<Param name = "include" value = "username, password"/>
</Convert>
</Allow>
<Signatures>
<! [CDATA [
Import java. util. List;
Import com. DWR. testclass;
Import com. DWR. testbean;
Testclass. testmethod7 (list <testbean> );
]>
</Signatures>
</DWR>
<Signatures> A tag is used to declare the exact class contained in the list, set, or map parameters in a Java method, so that Java code can make judgments.
7.2 calls in JavaScript (taking the returned list as an example, the list element is testbean)
First, introduce JavaScript scripts
Second, write JavaScript Functions that call Java methods.
Function calltestmethod7 (){
// Define the parameters to be passed to the Java method
VaR data;
// Construction parameter. date 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 );
}
Note:
1. In 6th cases, if the returned value of the Java method is map, the following processing is performed in the Javascript callback function that receives the returned value:
Function callbackfortestmethod (data ){
// The Return Value of the date receiving Method
For (VAR property in data ){
VaR bean = data [property];
Alert (bean. username );
Alert (bean. Password );
}
}
2. In 7th cases, if the Java method parameter is map (assuming that its key is string and value is testbean ), the following method is used to construct the parameters to be passed in the JavaScript function that calls this method:
Function calltestmethod (){
// Define the parameters to be passed to the Java method
VaR data;
// Construct a parameter. 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 );
}
Add the following configuration segments to DWR. xml:
<Signatures>
<! [CDATA [
Import java. util. List;
Import com. DWR. testclass;
Import com. DWR. testbean;
Testclass. testmethod7 (Map <string, testbean> );
]>
</Signatures>
3. From the above, we can find that when the return value of the Java method is list (SET), DWR converts it to an object array and passes JavaScript; when the return value of the Java method is map, DWR converts it into an object. The attribute of the object is the key value of the original map, and the attribute value is the value corresponding to the original map.
4. If the parameters of the Java method are list (SET) and MAP, JavaScript also constructs the corresponding JavaScript data to pass to Java based on the three parameters.

8. If spring + DWR is used

Add

<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>

And the configuration in DWR is:

<DWR>
<Allow>
<Create creator = "Spring" javascript = "testclass">
<Param name = "beanname" value = "userbiz"/>
<Include method = "testmethod4"/>
</Create>
<Convert converter = "Bean" match = "" com. DWR. testbean ">
<Param name = "include" value = "username, password"/>
</Convert>
</Allow>
</DWR>

Others are the same as above

Related Article

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.