DWR is a framework. Simply put, it can directly call java methods in javascript without having to write a lot of javascript code. Its implementation is based on ajax and can achieve no refresh effect.
There are many examples of DWR on the Internet, but most of them are just calls of a certain method. This article only introduces DWR at the usage level and does not involve more technology and design. Its purpose is to allow beginners to learn it quickly. How various java methods are called in javascript.
1. dwr configuration web.xml
1. Minimum configuration
<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 automatically generated by DWR (Using debug/test mode), we can add
in the servlet configuration<init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param>
This parameter DWR defaults to false. If you select true, we can see each DWR class you deployed through http://localhost:port/app/dwr. And you can test whether each method of java code is running normally. For security reasons, you must set this parameter to false in a formal environment.
3. Configuration of multiple dwr.xml files
There may be several situations, let’s 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> </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-name, then 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-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 based on J2EE security and add different roles for different URLs.
2. Dwr usage
1. Call a JAVA method with no 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>
tag contains 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 the instance of the class to be remoted. The creator="new" attribute specifies the generation method of the Java class instance. New means that DWR should call the default constructor of the class to obtain the instance. Others include the spring method, which obtains the instance by integrating with the IOC container Spring, etc. The javascript=" testClass " attribute specifies the name used by javascript code to access the object.
The tag specifies the java class name to be exposed to javascript.
Thetag specifies the methods to be exposed to JavaScript. If not specified, all methods will be exposed.
The tag specifies the method to be prevented from being accessed.
1.2. Calling
in javascriptFirst, introduce the javascript script
<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 the script files that come with dwr.
Secondly, write a javascript function that calls the java method
Function callTestMethod1(){ testClass.testMethod1(); }
2. Call java methods with simple return values
2.1. Configuration of dwr.xml
The 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. Calling
in javascriptFirst, introduce the javascript script
Secondly, write the javascript function that calls the java method and the callback function that receives the return value
Function callTestMethod2(){ testClass.testMethod2(callBackFortestMethod2); } Function callBackFortestMethod2(data){ //其中date接收方法的返回值 //可以在这里对返回值进行处理和显示等等 alert("the return value is " + data); }
Where callBackFortestMethod2 is the callback function that receives the return value
3. Call a 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. Calling
in javascriptFirst, introduce the javascript script
Secondly, write a javascript function that calls the java method
Function callTestMethod3(){ //定义要传到java方法中的参数 var data; //构造参数 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 c match=""com.dwr.TestBean"> <param name="include" value="username,password" /> </convert> </allow> </dwr>
标签负责公开用于Web远程的类和类的方法,标签则负责这些方法的参数和返回类型。convert元素的作用是告诉DWR在服务器端Java 对象表示和序列化的JavaScript之间如何转换数据类型。DWR自动地在Java和JavaScript表示之间调整简单数据类型。这些类型包括Java原生类型和它们各自的封装类表示,还有String、Date、数组和集合类型。DWR也能把JavaBean转换成JavaScript 表示,但是出于安全性的原因,要求显式的配置,标签就是完成此功能的。c属性指定转换的方式采用JavaBean命名规范,match=""com.dwr.TestBean"属性指定要转换的javabean名称,标签指定要转换的JavaBean属性。
4.2、javascript中调用
首先,引入javascript脚本
其次,编写调用java方法的javascript函数和接收返回值的回调函数
其中callBackFortestMethod4是接收返回值的回调函数
5、调用有JavaBean参数的java方法
5.1、dwr.xml的配置
<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、javascript中调用
首先,引入javascript脚本
其次,编写调用java方法的javascript函数
Function callTestMethod5(){ //定义要传到java方法中的参数 var data; //构造参数,date实际上是一个object data = { username:"user", password:"password" } testClass.testMethod5(data); }
并且在dwr.xml中增加如下的配置段
<signatures> <![CDATA[ import java.util.List; import com.dwr.TestClass; import com.dwr.TestBean; TestClass.testMethod7(Map<String,TestBean>); ]]> </signatures>
3、由以上可以发现,对于java方法的返回值为List(Set)的情况,DWR将其转化为Object数组,传递个javascript;对于java方法的返回值为Map的情况,DWR将其转化为一个Object,其中Object的属性为原Map的key值,属性值为原Map相应的value值。
4、如果java方法的参数为List(Set)和Map的情况,javascript中也要根据3种所说,构造相应的javascript数据来传递到java中。