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>