Using AJAX with direct WEB remoting

Source: Internet
Author: User
Tags object contains expression functions include object model return client
Ajax|web
"Guided reading" This article describes the ability to formalize a remote invocation of a server-side Java object from JavaScript client code by using AJAX with the direct WEB remoting.

DWR Introduction

From the simplest point of view, DWR is an engine that exposes the method of server-side Java objects to JavaScript code. Using DWR can effectively eliminate all of the AJAX request-response loops from the application code. This means that client code no longer needs to deal directly with the XMLHttpRequest object or the server's response. You no longer need to write serialization code for an object or use a Third-party tool to turn an object into XML. You don't even need to write a servlet code to adjust the Ajax request to a call to a Java domain object.

DWR is an open source solution that uses the Apache license agreement, which includes a server-side Java library, a DWR servlet, and a JavaScript library. Although DWR is not the only AJAX-RPC toolkit available on the Java platform, it is the most mature and provides a number of useful features.

DWR is deployed as a servlet in a WEB application. As a black box, this servlet has two main roles: first, for each class exposed, DWR dynamically generates JAVASCRIPT contained in a Web page. The generated JavaScript contains stub functions that represent the corresponding methods on the Java class and perform xmlhttprequest behind the scenes. These requests are sent to DWR, whose second role is to translate the request into a method call on a server-side Java object and send the method's return value in a servlet response to be sent back to the client, encoded as JavaScript. DWR also provides JavaScript tool functions that help you perform common user interface tasks.

About the example

Before I explain DWR in more detail, I want to introduce a simple example scenario. As in the previous article, I'll take a minimal model based on an online store that contains a basic product representation, a user's cart that can contain product items, and a data Access object (DAO) that queries products from the data store. The Item class is the same as used in the previous article, but no manual serialization method is implemented. Figure 1 illustrates this simple setting:

Figure 1. Class diagram that describes the Cart, Catalogdao, and Item classes

In this scenario, I'll demonstrate two very simple use cases. First, users can perform text searches in the directory and view matching items. Second, users can add goods to the shopping cart and view the total price of the goods in the cart.

Implementation Directory

The starting point for the DWR application is to write the server-side object model. In this example, I started by writing DAO to provide search capabilities for the product catalog data store. Catalogdao.java is a simple stateless class that has a parameterless constructor. Listing 1 shows the signature of the Java method I want to expose to Ajax customers:

Listing 1. The Catalogdao method that is exposed through DWR

/**  * Returns A list of items in the catalog that have   *  names or descriptions matching the search expression< c6/>* @param expression Text to search for in item names   * and  descriptions   * @return List of all matching ite MS */Public List 
          
            Finditems (String expression),/** * Returns the item corresponding to a given Item Id
           * @param id The ID code of the item  * @return The matching item  
          

Next, I need to configure DWR to tell it that Ajax customers should be able to build Catalogdao and invoke these methods. I do these things in the Dwr.xml configuration file shown in Listing 2:

Listing 2. Exposing the configuration of the Catalogdao method

! DOCTYPE dwr public   "-//getahead limited//dtd Direct Web Remoting 1.0//en"   "http://www.getahead.ltd.uk/dwr/ Dwr10.dtd "> 
           
            
             
              
              
               /create> 
               
                
               
             
            
           
          

The root element of the Dwr.xml document is DWR. Within this element is the Allow element, which specifies the class to be DWR for remote. The two child elements of the Allow are create and convert.

<

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.