DWR (Direct Web remoting) is an open-source Ajax framework for remote servers to improve web page interaction with Java.
(1) Import DWR. jar, commons-logging-1.2.jar
(2) Add in the web. xml file
<! -- Configure the core servlet of DWR --> <servlet-Name> DWR-invoker </servlet-Name> <servlet-class> Org. directwebremoting. servlet. dwrservlet </servlet-class> <init-param> <param-Name> debug </param-Name> <param-value> true </param-value> </init- param> <! -- You do not need to configure the following two items --> <init-param> <param-Name> crossdomainsessionsecurity </param-Name> <param-value> false </param-value> </init -param> <init-param> <param-Name> allowscripttagremoting </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>
(3) create the DWR. xml file in the WEB-INF folder
<! Doctype DWR public "-// getahead limited // DTD direct Web remoting 3.0 //" http://getahead.org/dwr/dwr30.dtd "> <DWR> <allow> <create creator =" new "javascript =" dwrtest "> <Param name =" class "value =" CN. axin. DWR. dwrtest "/> <! -- Custom class --> </create> <create creator = "new" javascript = "ajaxdate"> <Param name = "class" value = "Java. util. date "/> </create> </allow> </DWR>
Dwrtest class
public class DWRTest {public int getData(int index){Random rand = new Random();return rand.nextInt(index);}}
By modifying the DWR. xml file, you can expose custom Java classes to JavaScript remote calls.
In this configuration file, the Creator attribute is required, and it specifies which generator to use. Creator
There are three options for attributes: new, scripted, and spring. The most commonly used is new, which indicates that Java
The default no-argument constructor of the class to create the Instance Object of the class. The scripted value indicates that the class is created using the script language.
Java class object; spring indicates creating Java class object through Spring framework Bean
You can also add the include tag under the create element to specify the method to be exposed to JavaScript.
(4) Enable
<SCRIPT type = "text/JavaScript" src = "DWR/engine. JS "> </SCRIPT> <SCRIPT type =" text/JavaScript "src =" DWR/util. JS "> </SCRIPT> <SCRIPT type =" text/JavaScript "src =" DWR/interface/dwrtest. JS "> </SCRIPT> <SCRIPT type =" text/JavaScript "src =" DWR/interface/ajaxdate. JS "> </SCRIPT> <SCRIPT type =" text/JavaScript "> function dotest1 () {ajaxdate. tostring (callback1);} function callback1 (data) // data is the return value of the tostring method of the date class {window. alert ("the current time is:" + data);} function dotest2 () {dwrtest. getdata (123, callback2); // 123 is the parameter of the getdata method, and callback2 is the callback function} function callback2 (data) // data is the return value of the getdata method {window. alert ("random number:" + Data) ;}</SCRIPT> Dwrtest. js and ajaxdate. js are named by your DWR. xml
<Create creator = "new" javascript = "dwrtest">
<Create creator = "new" javascript = "ajaxdate">
JavaScript property configuration name
Note:
<SCRIPT type = "text/JavaScript" src = "DWR/engine. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "DWR/util. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "DWR/interface/dwrtest. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "DWR/interface/ajaxdate. js"> </SCRIPT>
It would be useless if we put these two lines in front, and the DWR official website is like this.
<SCRIPT src = '/[YOUR-WEBAPP-CONTEXT]/DWR/interface/[Your-script]. js'> </SCRIPT>
<SCRIPT src = '/[YOUR-WEBAPP-CONTEXT]/DWR/engine. js'> </SCRIPT>
This problem can hurt me ..........
Maybe this is not the case for you. You can understand it only after testing.
Use of DWR