[Ajax] Summary of DWR usage

Source: Internet
Author: User
Tags cdata

I learned DWR over the past two days. Now let's summarize it.
DWR is a framework that makes it easy to connect JS and Java Using ajax, and exposes the methods of the server-side Java objects to JavaScript code.
If you are using dwr2.0 jar package, you also need to import log4j. jar and commons-loggin.jar at the same time, do not forget !!
Web. xml and DWR. XML are placed under the WEB-INF!
-----------------------------
Configure web. xml: when the web project is started, it will find the path of the specific class used in it, and load it accordingly.

<Servlet>
<Servlet-Name> DWR-invoke </servlet-Name>
<Servlet-class> UK. Ltd. getahead. DWR. dwrservlet </servlet>
<Init-param> // This is for debugging. If it is officially released, set this parameter to false. However, the default value of 2.0 is true.
<Param-Name> debug </param-value>
<Param-value> true </param-value>
</Init-param>
<Init-param> // This is required by dwr2.0. Otherwise, java. Lang. illegalargumentexception will be reported.
<Param-Name> classes </param-value>
<Param-value> JAVA. Lang. Object </param-value>
</Servlet>

<Servlet-mapping>
<Servlet-Name> DWR-invoke </servlet-Name>
<URL-pattern>/DWR/* </url-pattern>
</Servlet-mapping>

-----------------------------
Configure DWR. XML: DWR. the purpose of XML is to let you tell DWR which classes and methods should be exposed to the front-end. When DWR is started. the XML file converts the methods in the Java class into available methods in the JS class so that the foreground can use them.

Note: The following statements are not fully written in dwr2.0. If dwr2.0 is used, classes exposed in Java (displayed in blue) cannot be written in the <create> attribute, the class exposed in <Param name = "class" value = "*** Java should be deleted and written before <include method =" "/>, you must write the complete path *** "/>.

<DWR>
<Allow>
<Create creator = "new" javascript = "*** class called by JS ***" class = "*** class exposed in Java, complete path must be written *** ">
<Include method = "**** methods to be exposed in the Java exposure class, if not written, all exposed by default ***"/>
</Create>
</Allow>
</DWR>

-----------------------------
Add in HTML or JS page
<SCRIPT src = "<% = basepath %> classes exposed in DWR/interface/Java"> </SCRIPT> // do not write in this way <SCRIPT ***/>.
<SCRIPT src = "<% = basepath %> DWR/engine. js"> </SCRIPT> // DWR script-driven JS, which must be written
<SCRIPT src = "<% = basepath %> DWR/util. js"> </SCRIPT> // This is a toolkit and can be left empty
<SCRIPT type = "text/JavaScript">
Function domethod ()
{
// Call method: if the public class is AAA, the method in the public AAA is BBB ([parameter]);
AAA. BBB ([parameter], callback); // callback function callback ()
}

Function callback (data) // data is the value returned by the background. The name can be omitted because JS allows
{
Solution ......; // If callback () does not specify the return value, it can be obtained through argments [0 ].
}
</SCRIPT>

In fact, we call AAA. BBB ([parameter], callback); that is, we do the following:
(The red text below is packaged and not on the front-end, so even if you haven't created HTML or JSP to call it, you can also use localhost: 8080/project name/DWR to see your exposed Java class for testing)
Function AAA (){};
AAA. BBB = function ([parameter], callback)
{
Dwrengine. _ execute ('/DWR/dwr', 'aaa', 'bbb', [parameter], callback );
}

In DWR. XML exposure method to get the method in JS, when we call AAA. when BBB is used,/DWR is transferred to dwrservlet to use AAA in Java. the BBB method, and then return the value to callback (passing through DWR. convert of XML)

----------------------------
We have roughly analyzed the work of DWR, and now we need to modify DWR. XML for different parameter types.

DWR automatically adjusts simple data types between Java and JavaScript representations, including Java Native types and their respective encapsulation class representations, as well as string, date, array, and set types. However, if the parameter type is not a simple data type, it must be converted.

TuneReturnJavaBeanOfJavaMethod

Add the <allow> label in DWR. xml

<Convert converter = "Bean" match = "*** in general, it is a JavaBean ***"> // int, String, list, etc. can be obtained by JS without explicit conversion.
<Param name = "include" value = "*** attributes in JavaBean, separated by ',' ***"/> // you can leave this sentence empty.
</Convert>

<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.

In this way, the data directly obtained by the callback function on the JS end is a JavaBean, and the bean property xxx can be obtained directly through data. xxx.

TuneAvailableJavaBean ParametersOfJavaMethod

The DWR. xml configuration is the same as above.

On the JS end, write the parameters to be passed in as JavaBean. For example, to pass in a stuan named student with the parameters name and password
VaR Stu = {name: "zhangsan", password: "zspassword"}; // This Is a json representation.
AAA. BBB (STU, callback );

TuneReturnList,SetOrMapOfJavaMethod

The DWR. xml configuration is the same as above. If the data in the collection is of the simple data type, you do not need to write <convert>

On the JS end, the data in the list is Bean. Data is a list type, and data can be obtained in sequence as long as the for loop is used.
Traversal method 1:
For (VAR I = 0; I <data. length; I ++)
/* If the return value of the Java method is list (SET), DWR converts it to an object array and passes JavaScript */
{
Alert (data [I]. Name + ":" + data [I]. Password );
}

Traversal method 2:
For (VAR property in data) // property is the serial number, starting from 0
{
VaR bean = data [property];
Alert (bean. Name + ":" + bean. Password );
}

The returned result is only a traversal of JavaBean.

If the return value of the Java method is map

For (VAR property in data) // property is the key value
/* If the returned value of the Java method is map, DWR converts it into an object,
Here, 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 */
{
VaR bean = data [property];
Alert (bean. username );
Alert (bean. Password );
}

If you know the key value, you can use: Data. Key to get the value.

TuneAvailableList,SetOrMapParameterJavaMethod

Add the <signatures> label to the <DWR> label of DWR. xml.

<Signatures> labels are used to declare the exact classes contained in the list, set, or map parameters in Java methods, so that Java code can make judgments. They are JS --> JAVA.

If the example parameter is a list of JavaBean, you only need to add [] When the parameter is constructed in the example of JavaBean, as shown below:

VaR Stu = [{name: "zhangsan", password: "zspassword" },{ name: "Lisi", password: "lspassword"}];
// Treat list as an array
AAA. BBB (STU, callback );

Add the following configuration section in DWR. xml. (You can do this without adding it)

<Signatures>
<! [CDATA [
Import java. util. List;
Import com. DWR. AAA; // The package path of AAA must be complete.
Import com. DWR. testbean; // JavaBean
AAA. BBB (list <testbean> );
]>
</Signatures>

For example, the parameter is map of JavaBean, the key is string, and the value is JavaBean, as shown below:
VaR Stu =

{
"Key1": {name: "zhangsan", password: "zspassword "},
"Key2": {name: "Lisi", password: "lspassword "}
};

AAA. BBB (STU, callback );

 

Add the following configuration section in DWR. xml. (You can do this without adding it)

<Signatures>
<! [CDATA [
Import java. util. List;
Import com. DWR. AAA; // The package path of AAA must be complete.
Import com. DWR. testbean; // JavaBean
AAA. BBB (Map <string, testbean> );
]>
</Signatures>

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.