1. Import Three jar packages:
Commons-validator-1.1.4.jar
Jakarta-oro-2.0.8.jar
DWR. Jar
2. modify the configuration file of Web. xml
<Servlet>
<Servlet-Name> DWR-invoker </servlet-Name> -------------------------- servletname
<Servlet-class> UK. Ltd. getahead. DWR. dwrservlet </servlet-class> ----- fixed value
<Init-param>
<Param-Name> debug </param-Name> ------- whether debugging is required
<Param-value> true </param-value>
</Init-param>
</Servlet>
<Servlet-mapping>
<Servlet-Name> DWR-invoker </servlet-Name>
<URL-pattern>/DWR/* </url-pattern> -------- requests from the Servlet
</Servlet-mapping>
3. Compile common Java background code
4. Write the class to be exposed to the front-end into the DWR. xml configuration file.
Make the following configuration under the allow node:
<Create creator = "new" javascript = "Demo"> ----- name required for calling the class on the foreground, for example, demo
<Param name = "class" value = "com. bluedot. Demo"/> ----- location of the class
</Create>
<Convert converter = "Bean" match = "com. bluedot. User"/> ----- if the returned value is a custom object, write the row.
5. test:
You can enter the path under the project name in the browser to view all the methods that can be called at the front-end when classes configured in DWR are exposed:
Example: http: // localhsot: 8080/dwrdemo/DWR
6. Foreground call:
Import JS files (usually at least 3)
<SCRIPT type = "text/JavaScript">
Function Update (){
VaR name = DWR. util. getvalue ("demoname"); ----------- get the value of the input field in the form
Demo. sayhello (name, function (data) {----------------- The name parameter is the parameter of the sayhello method in the demo class, and function {} is the callback function.
DWR. util. setvalue ("demoreply", data );
});
Demo. Say (function (User) {---------------- when the called method does not have an input parameter, only one callback method is passed.
Alert (user. ID );
Alert (user. Name );
Alert (user. Birthday );
});
VaR OBJ = new object (); ----------------- when the called method is a custom object, the DWR framework transmits the JS object to the background in JSON format.
OBJ. ID = 2;
OBJ. Name = 'wang wu ';
Demo. saysay (OBJ, function (data ){
Alert (data );
});
}
</SCRIPT>