Ajax/DWR/struts instance Development (below)

Source: Internet
Author: User

This part will actually develop an application, apply a foreign sample template, and make a simple version of online gifts.
First, let's talk about the application structure.
Page:
Demo. jsp is used to display the interface
Server:
Demoaction is the action subclass in struts to control the conversion,
Demofacade is the business class responsible for business processing.
Goods is a business entity class

The struts configuration is ignored. In fact, this application does not contain the struts configuration.
To configure DWR, you must first Add the following servelt ing in Web. xml:
<Servlet>
<Description> direct Web remoter servlet </description>
<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>
As shown above, UK. Ltd. getahead. DWR. dwrservlet is the core of DWR and is used to handle JavaScript remote method calls. For other parameters, see related documents.
Then, you need to configure the corresponding remote method in DWR. XML (related to the method that needs to be called on the client). The following shows the application configuration:
<DWR>
<Allow>
<Convert converter = "Bean" match = "DWR. Demo. Goods"/>
<Create creator = "new" javascript = "demoaction" class = "DWR. Demo. demoaction">
<Include method = "query4dwr"/>
<Include method = "copy4dwr"/>
<Include method = "paste4dwr"/>
</Create>
<Create creator = "new" javascript = "demofacade" class = "DWR. Demo. demofacade">
<Include method = "querylist"/>
<Include method = "Restore"/>
<Include method = "Del"/>
</Create>
</Allow>
</DWR>
A converter is used to map DWR. demo. goods is of the bean type. For other converter types, see the relevant documentation. There are also two creators that create the demoaction class and demofacade class in Javascript, respectively, corresponding to DWR. demo. demoaction class and DWR. demo. demofacade, where the defined method can be directly called from JavaScript.
Finally, we need to include the corresponding Javascript in the page:
<SCRIPT src = 'dwr/interface/demoaction. js'> </SCRIPT>
<SCRIPT src = 'dwr/interface/demofacade. js'> </SCRIPT>
<SCRIPT src = 'dwr/engine. js'> </SCRIPT>
<SCRIPT src = 'dwr/util. js'> </SCRIPT>
As shown above, DWR/interface/demoaction. JS and DWR/interface/demofacade. JS is a Javascript file automatically generated by DWR, including the corresponding classes and methods, DWR/engine. JS is the core engine script of DWR to process client call conversion, DWR/util. JS contains tool functions to simplify page processing.

The following uses a query as an example to describe how to use DWR:
Demoaction:
Public list query4dwr (INT type, Boolean needclear, httpservletrequest request ){
If (needclear) request. getsession (). removeattribute ("DWR. Demo. goodsid ");
Return demofacade. querylist (type );
}
Demo. jsp:
Function updateresults (){
Dwrutil. removeallrows ("goodsbody ");
VaR type = Document. getelementbyid ("type"). value;
Demoaction. query4dwr (type, true, filltable );
}
Function filltable (goods ){
Document. Forms [0]. Select. Checked = false;
Document. getelementbyid ("totalrecords"). innerhtml = goods. length;
Dwrutil. addrows ("goodsbody", goods, [addcheckbox, getname, getprice, getcount]);
}
As shown above, demoaction. query4dwr (type, true, filltable) can directly call the demoaction method. Here, filltable is a function, and DWR performs subsequent processing through the callback function. Compare the method parameters in JavaScript and action. httpservletrequest can be left blank. DWR will automatically add the parameter, and the callback parameter will be placed at the end. This is a good method. For other methods, see the document.

Finally, let's take a look at how DWR is integrated with Struts by using the following code:
Public actionforward query (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {
String type = request. getparameter ("type ");
// Or select from Form

List goodslist = query4dwr (integer. parseint (type), true, request );
Request. setattribute ("goodslist", goodslist );

Return Mapping. findforward ("success ");
}

Public list query4dwr (INT type, Boolean needclear, httpservletrequest request ){
If (needclear) request. getsession (). removeattribute ("DWR. Demo. goodsid ");
Return demofacade. querylist (type );
}
The previous method is struts, but DWR does not support it. Therefore, we need to refactor the method below before it can be called by DWR.
In fact, you only need to use httpservletrequest in the method to reconstruct the method. If you do not use httpservletrequest, you can directly call the class method at the business layer. This is simple and convenient, as shown below:
Demo. jsp:
Function restore (){
Demofacade. Restore (updateresults );
}

Demoaction:
Public synchronized void restore (){
Goodslist. Clear ();
Initgoods ();
}

Summary

DWR encapsulates the modules used to interact with the server in Ajax, and simplifies the interaction between the client and the server by directly calling the server class. Although tag-like components are also defaulted, Ajax development has been greatly simplified.

Resources
1. sample source code: Download
2. DWR main site: http://getahead.ltd.uk/dwr
3. Ajax main site: http://en.wikipedia.org/wiki/AJAX

 
Related Article

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.