Steps for using DWR

Source: Internet
Author: User
DWR (Direct Web remoting) is an open-source remote server-side Ajax framework used to improve web page interaction with Java. It can help developers develop websites that contain Ajax technology. It allows the code in the browser to use Java functions running on the Web server, just as it is in the browser.

1. Basic steps:
1. Add jar package for the project: DWR. Jar common-logging.jar
If struts is used in this project, Struts has built-in common-logging.jar and does not need to be introduced again
2. Add the servlet configuration of DWR in the web. XML section.
<Servlet>
<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>
</Servlet>
<Servlet-mapping>
<Servlet-Name> DWR-invoker </servlet-Name>
<URL-pattern>/DWR/* </url-pattern>
</Servlet-mapping>
Note:
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> true </param-value>
</Init-param>
This configuration item is used to enable the DWR console. If this item is set to false, the DWR console is invalid.
3. Create DWR. xml under/WEB-INF
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype DWR public "-// getahead limited // DTD direct Web remoting 2.0 //" http://getahead.ltd.uk/dwr/dwr20.dtd ">
<DWR>
<Allow>
<Create creator = "new" javascript = "test">
<Param name = "class" value = "test. testdao"/>
</Create>
<! -- If the object class is required, the following match must be configured: Class path converter = "Bean": fixed parameter -->
<Convert match = "bean. *" converter = "Bean"> </convert>
</Allow>
</DWR>
Note: Creator = "new" indicates how this class is created.
Javascript = "test" indicates the name of this class in JavaScript.
Name = "class" indicates configuring a class to DWR value = "": indicates the class path.
4. Create related classes:
Package test;
Public class testdao {
Public void Hello (){
System. Out. println ("Hello JavaScript ...");
}
Public void sayhello (string name ){
System. Out. println ("hello" + name );
}
Public int add (int A, int B ){
Return A + B;
}
}
5. Deploy the project to Tomcat and test whether the DWR environment is complete through the following address:
Http: // ip: Port/appname/DWR
If you see the console, click to enter to test whether the method can run normally
6. On the page for calling resources related to DWR, follow the console prompts to renew resources:
<SCRIPT type = 'text/JavaScript 'src = '$ {pagecontext. Request. contextpath}/DWR/interface/test. js'> </SCRIPT>
<SCRIPT type = 'text/JavaScript 'src = '$ {pagecontext. Request. contextpath}/DWR/engine. js'> </SCRIPT>
7. script method for calling DWR resources:
DWR name. Method Name (parameter ...,{
Callback: function (Return Value ){
Process the returned Logical Block
}
})
For example:
Test. Hello ();
Test. sayhello ("weird kid ");
Test. Add (10, 10 ,{
Callback: function (data ){
Alert (data );
}
});
Test. Add (10, 10, callback );
Function callback (data ){
Alert (data );
}

Ii. Basic applications:
Response data parsing:
1. Simple Object:
Test. gettest ({
Callback: function (data ){
Alert (data. ID + ":" + data. Name + ":" + data. Age + ":" + data. Sex + ":" + data. Birthday );
}
});
2. Simple list:
Test. queryforlist ({
Callback: function (data ){
For (VAR I = 0; I <data. length; I ++ ){
Alert (data [I]);
}
}
});
3. Object List:
Test. queryall ({
Callback: function (data ){
For (VAR I = 0; I <data. length; I ++ ){
Alert (data [I]. ID + ":" + data [I]. name + ":" + data [I]. age + ":" + data [I]. sex + ":" + data [I]. birthday );
}
}
});
4. Upload objects to the background:
VaR myobject = {'id': 108, 'name': 'Small

White ', 'age': 24, 'sex': 'female', 'birthday': '2017-11-01 '};
Test. setobject (myobject );

Parameter Parsing:
Prehook: Run before interaction ends
Callback: return after interaction ends
Posthook: run after the return is complete
Timeout: Set the response timeout.
Errorhandler: sets exception capture
Test. Query ({
Callback: function (data) {alert ("processing return" + Data );},
Prehook: function () {alert ("before returning ...");},
Timeout: 4000,
Errorhandler: function (MSG) {alert ("if the response time exceeds the timeout value, the server times out ...");}
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.