DWR is a framework to simply call Java methods directly in JavaScript without having to write a whole bunch of JavaScript code. Its implementation is based on Ajax, can achieve no refreshing effect.
There are many examples of dwr on the Internet, but most of them are only the call of some methods, this article only introduces DWR on the level of use, does not involve more technology and design, the aim is to enable beginners to quickly learn how various Java methods are invoked in JavaScript.
I. The web.xml of DWR configuration articles
1. Minimum match
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class> uk.ltd.getahead.dwr.dwrservlet</servlet-class>
</servlet>
<servlet-mapping>
< servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</ Servlet-mapping>
2, when we want to see the DWR automatically generated test page (Using debug/test mode), you can add in the servlet configuration
<init-param>
<param-name>debug</param-name>
<param-value>true</ Param-value>
</init-param>
This parameter dwr is false by default. If you choose True, we can see each DWR class you deploy through Http://localhost:port/app/dwr. And you can test whether every method of Java code is working correctly. For security reasons, you must set this parameter to false in the formal environment.
3, the configuration of multiple Dwr.xml files
There may be several cases that we enumerate. A servlet, multiple dwr.xml configuration files, multiple servlet, one or more dwr.xml configuration files per servlet.
3.1, a servlet, multiple dwr.xml configuration files
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class> uk.ltd.getahead.dwr.dwrservlet</servlet-class>
<init-param>
<param-name>config-1< /param-name>
<param-value>WEB-INF/dwr1.xml</param-value>
</init-param>
< init-param>
<param-name>config-2</param-name>
<param-value>WEB-INF/dwr2.xml< /param-value>
</init-param>
</servlet>
In this configuration, the value of the param-name must begin with CONFIG. Param-name can have >=0. If there is no param-name, then the web-inf/dwr.xml will be read. If there are more than 0 param-name, the Web-inf/dwr.xml file will not be read.
3.2, multiple servlet, each servlet corresponds to one or more dwr.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class> uk.ltd.getahead.dwr.dwrservlet</servlet-class>
</servlet>
<servlet>
< Servlet-name>dwr-invoker1</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.dwrservlet</ servlet-class>
<init-param>
<param-name>config-admin</param-name>
< param-value>web-inf/dwr1.xml</param-value>
</init-param>
<init-param>
< param-name>debug</param-name>
<param-value>true</param-value>
</init-param >
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name >
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet-mapping >
<servlet-name>dwr-invoker1</servlet-name>
<url-pattern>/dwr1/*</ Url-pattern>
</servlet-mapping>
In this case, we can control the permissions according to the Java security, for different URLs, add different roles.
Second, dwr use of articles
1. Invoke Java methods without return values and parameters
1.1, the Dwr.xml configuration
<dwr>
<allow>
<create creator= "new" javascript= "TestClass" >
<param Name= "class" Value= "Com.dwr.TestClass"/>
<include method= "testMethod1"/>
</create>
</allow>
</dwr>
The tag includes something that you can expose to JavaScript access.
tag to specify Java classes that can be accessed in JavaScript and define how DWR should obtain an instance of the class to be remote. The creator= "new" attribute specifies how Java class instances are generated, and new means that DWR should invoke the default constructor of the class to obtain an instance, as well as spring, which is integrated with the IOC container spring to obtain instances, and so on. The javascript= "TestClass" property specifies the name that JavaScript code uses when accessing an object.
The label specifies the name of the Java class to be exposed to JavaScript.
The label specifies the method to expose to JavaScript. Expose all methods if you do not specify them.
The label specifies the method that you want to prevent from being accessed.
1.2. Calling 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>
Where Testclass.js is dwr automatically generated from the configuration file, Engine.js and Util.js are the Dwr script files.
Second, write JavaScript functions that invoke Java methods
Function callTestMethod1 () {
testclass.testmethod1 ();
}
2. Invoke Java method with simple return value
2.1, the Dwr.xml configuration
Configured with 1.1
<dwr>
<allow>
<create creator= "new" javascript= "TestClass" >
<param name= " Class "value=" Com.dwr.TestClass "/>
<include method=" testMethod2 "/>
</create>
</ Allow>
</dwr>
2.2. Calling in JavaScript
First, introduce JavaScript scripts
Second, write JavaScript functions that invoke Java methods and callback functions that receive return values
Function callTestMethod2 () {
testclass.testmethod2 (CALLBACKFORTESTMETHOD2);
}
Function callBackFortestMethod2 (data) {/
/where the return value of the date Receive method
//Can be processed and displayed here, etc.
alert ("The return value is "+ data);
}
Where CALLBACKFORTESTMETHOD2 is the callback function that receives the return value
3. Invoke Java method with simple parameters
3.1, the Dwr.xml configuration
Configured with 1.1
<dwr>
<allow>
<create creator= "new" javascript= "TestClass" >
<param name= " Class "value=" Com.dwr.TestClass "/>
<include method=" testMethod3 "/>
</create>
</ Allow>
</dwr>
3.2. Calling in JavaScript
First, introduce JavaScript scripts
Second, write JavaScript functions that invoke Java methods
Function callTestMethod3 () {
//defines the parameter var data to be uploaded to the Java method
;
Construct parameter
data = "Test String";
TESTCLASS.TESTMETHOD3 (data);
}
4, invoke the return JavaBean Java method
4.1, the Dwr.xml configuration
<dwr>
<allow>
<create creator= "new" javascript= "TestClass" >
<param name= " Class "value=" Com.dwr.TestClass "/>
<include method=" testMethod4 "/>
</create>
< Convert c match= "Com.dwr.TestBean" >
<param name= "include" value= "Username,password"/> </
convert>
</allow>
</dwr>
The label is responsible for exposing the methods for the classes and classes used for Web remote, and the label is responsible for the parameters and return types of those methods. The role of the convert element is to tell Dwr how to transform the data type between the server-side Java object Representation and the serialized JavaScript. DWR automatically adjusts the simple data type between Java and JavaScript representations. These types include Java native types and their respective encapsulation class representations, as well as String, Date, array, and collection types. DWR can also translate JavaBean into JavaScript representations, but for security reasons, requiring an explicit configuration, the tag is the complete feature. The C property specifies how the transformation takes the JavaBean naming convention, match= the "Com.dwr.TestBean" attribute specifies the JavaBean name to convert, and the label specifies the JavaBean property to convert.
4.2. Calling in JavaScript
First, introduce JavaScript scripts
Second, write JavaScript functions that invoke Java methods and callback functions that receive return values
Where CALLBACKFORTESTMETHOD4 is the callback function that receives the return value
5, invoke the Java method with JavaBean parameters
5.1, the Dwr.xml configuration
<dwr>
<allow>
<create creator= "new" javascript= "TestClass" >
<param Name= "class" Value= "Com.dwr.TestClass"/>
<include method= "TestMethod5"/>
</create>
<convert C Match= "Com.dwr.TestBean" >
<param name= "include" value= "Username,password"/>
</convert>
</allow>
</dwr>
5.2. Calling in JavaScript
First, introduce JavaScript scripts
Second, write JavaScript functions that invoke Java methods
Function CallTestMethod5 () {
//defines the parameter var data to be uploaded to the Java method
;
Construct parameter, date is actually an object
data = {username: "user", Password: "Password"}
testclass.testmethod5 (data);
}
and add the following configuration segment in the 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 can be found, for the Java method return value of the list (Set), dwr convert it into an object array, passing a JavaScript; For a Java method whose return value is map, DWR converts it to an object. The property of object is the key value of the original map, and the property value is the corresponding value value of the original map.
4. If the parameters of the Java method are list (Set) and map, JavaScript should be constructed according to 3 types of JavaScript data to be passed into Java.