DWR is a framework, which is simply able to call java methods directly in javascript without having to write a lot of javascript code. Its implementation is based on ajax and can achieve refresh-free effect.
There are many examples of DWR on the Internet, but most of them are just calls of some method. This article only introduces DWR at the usage level, and does not involve more technology and design. The purpose is to enable beginners to quickly learn how various Java methods are called in JavaScript.
1. dwr configuration article web.xml
1. Minimum matching
<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 test page (Using debug/test mode) automatically generated by DWR, we can add it to 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 via http://localhost:port/app/dwr. And you can test whether each method of the java code runs normally. For safety reasons, you must set this parameter to false in a formal environment.
3. Configuration of multiple dwr.xml files
There may be several situations, we will list them one by one. One servlet, multiple dwr.xml configuration files; multiple servlets, each servlet corresponds to one or more dwr.xml configuration files.
3.1. One 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> <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 param-name must start with config. param-name can have >= 0. If there is no param-name, WEB-INF/dwr.xml will be read. If there are more than zero param-names, the WEB-INF/dwr.xml file will not be read.
3.2. Multiple servlets, 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-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 permissions according to J2EE security, and add different roles for different URLs.
2. DWR usage
1. Calling JAVA method without return value and parameters
1.1. Configuration of dwr.xml
<dwr><allow><create creator="new" javascript="testClass" ><param name="class" value="com.dwr.TestClass" /><include method="testMethod1"/></create></allow></dwr>
Tags include things that can be exposed to javascript access.
The tag specifies the java class that can be accessed in javascript and defines how DWR should obtain instances of the class to be remote. The creator="new" attribute specifies the generation method of java class instances. New means that DWR should call the default constructor of the class to obtain the instance. Others include spring methods, which can be integrated with the IOC container Spring to obtain the instance, 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 method to be exposed to javascript. If not specified, all methods will be disclosed.
The tag specifies the method to prevent access.
1.2. Called 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>
Among them, TestClass.js is automatically generated by dwr based on the configuration file, and engine.js and util.js are script files that come with dwr.
Secondly, write a javascript function that calls a java method
Function callTestMethod1(){ testClass.testMethod1();}2. Call java method with simple return value
2.1. dwr.xml configuration
The same configuration 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. Called in javascript
First, introduce javascript scripts
Secondly, write a javascript function that calls a java method and a callback function that receives the return value.
Function callTestMethod2(){testClass.testMethod2(callBackFortestMethod2);}Function callBackFortestMethod2(data){//where the return value of the date receives the method//The return value can be processed and displayed here, etc. alert("the return value is " + data);}where callBackFortestMethod2 is a callback function that receives the return value
3. Call java method with simple parameters
3.1. Configuration of dwr.xml
The same configuration 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. Called in javascript
First, introduce javascript scripts
Secondly, write a javascript function that calls a java method
Function callTestMethod3(){//Define the parameter var data to be passed to the java method;//Construct the parameter data = "test String";testClass.testMethod3(data);}4. Call the java method that returns JavaBean
4.1. 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>
Tags are responsible for exposing classes and methods used for web remotes, while tags are 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 representations. 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, it requires explicit configuration, and the tags perform this function. The c attribute specifies the conversion method to use the JavaBean naming specification, the match=""com.dwr.TestBean" attribute specifies the name of the Javabean to be converted, and the tag specifies the JavaBean attribute to be converted.
4.2. Called in javascript
First, introduce javascript scripts
Secondly, write a javascript function that calls a java method and a callback function that receives the return value.
where callBackFortestMethod4 is a callback function that receives the return value
5. Call java method with JavaBean parameters
5.1. 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. Called in javascript
First, introduce javascript scripts
Secondly, write a javascript function that calls a java method
Function callTestMethod5(){ //Define the parameter var data to be passed to the java method; //Construct the parameter, date is actually an object data = { username:"user", password:"password" } testClass.testMethod5(data);}And add the following configuration segment 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 for the case where the return value of the java method is List(Set), DWR converts it into an Object array and passes a javascript; for the case where the return value of the java method is Map, DWR converts it into an Object, where the Object attribute is the key value of the original Map and the attribute value is the corresponding value of the original Map.
4. If the parameters of the java method are List(Set) and Map, javascript should also construct corresponding javascript data to pass into java according to three types of things.