First knowledge of DWR

Source: Internet
Author: User

DWR (Direct Web remoting) is short for DWR. It is implemented based on Ajax and can achieve no refreshing effect. The biggest advantage of this framework is that it can call Java methods in JS, PASS Parameters, and get the return value. It is an open-source (APACHE) product. Why do we need to use DWR? First, we will introduce the basic Ajax process, from which we can see what benefits DWR brings.

1. Basic Introduction to Ajax

Ajax (Asynchronous JavaScript and XML) describes a development technology that uses HTML (or XHTML) and Cascading Style Sheets as expression information to create interactive web applications; the Document Object Model (DOM), JavaScript, dynamic display and interaction with the expression information, and XMLHttpRequest objects exchange and process data asynchronously with the Web server.

With Ajax, we can develop B/S programs with good interactivity. Meanwhile, the local and asynchronous Ajax refresh features greatly reduce the pressure on servers and networks. For non-Ajax B/S programs, HTML, Dom, and JS are also indispensable key technologies. Therefore, the introduction of Ajax technology mainly increases the use of XMLHttpRequest objects. For more information about Ajax and related important implementation code, see my blog: http://blog.csdn.net/smszhuang168/article/details/7742402.

2. Basic Introduction to The DWR framework

From the simplest point of view, DWR is an engine that can expose methods of server-side Java objects to JavaScript code. Using DWR can effectively eliminate all Ajax request-response loops from application code. This means that the client Code no longer needs to directly process the XMLHTTPRequest object or server response. You no longer need to compile Object serialization code or use a third-party tool to convert an object into XML. You do not even need to write servlet code to adjust Ajax requests to call Java domain objects.

DWR is deployed as a servlet in a web application. Think of it as a black box. This servlet has two main functions: first, for each public class, DWR dynamically generates JavaScript contained in the web page. The generated JavaScript contains the stub function, representing the corresponding methods in the Java class and executing XMLHttpRequest behind the scenes. These requests are sent to DWR. In this case, its second role is to translate the requests into method calls on the server-side Java object and put the return values of the methods in
Send the servlet response back to the client and encode it into JavaScript. Shows the standard process of DWR:


The eventhandler () event is triggered on the page. The ajaxservice. getoptions method is called internally. After the call is complete, the data returned by the server is displayed using the client's populatelist () method.

We use a simple DWR example to illustrate how to use DWR.

Step 1: Download The DWR package.

Jar files of DWR need to be written to the WEB-INF/lib directory of the Web application (available at http://getahead.org/dwrdownload)

Step 2: configure the Web. xml file.

In the <web-app> </Web-app> tab of the web. xml file, add the following Configuration:

<servlet>   <servlet-name>dwr-invoker</servlet-name>   <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class></servlet><servlet-mapping>    <servlet-name>dwr-invoker</servlet-name>    <url-pattern>/dwr/*</url-pattern></servlet-mapping>

Step 3: configure the DWR. xml file.

Create a DWR. xml file in your application's WEB-INF with the following content:

<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTDDirect Web Remoting 2.0//EN" "http://getahead.ltd.uk/dwr/dwr20.dtd"><dwr> <allow>       <create creator="new" javascript="Pict" scope="application">      <param name="class"  value="bean.Pict"/>      <includ emethod="method1" />   </create>          </allow></dwr>

Here is a rough explanation:

Creator = "new" attribute specifies how Java class instances are generated. "New" means that DWR should call the default constructor of the class to obtain the instance of the class. Generally, this can be done.

Javascript = "PICT" specifies the name used when JavaScript code accesses a Java object.

The tag specifies the Java class name to be exposed to JavaScript.

<Include> the label specifies the method to be published to Javascript. If not specified, all methods are published.

Step 4: Call in JS

1) Introduce JavaScript scripts

<script language="javascript1.2" src="dwr/interface/Pict.js"></script><script language="javascript1.2" src="dwr/util.js"></script><script language="javascript1.2" src="dwr/engine.js"></script>

Note:

The first imported tag script must be manually written. The name is the same as the Javascript value in the <create> tag and Its suffix is. js. You only need to manually write the content here. In fact, DWR automatically generates the content based on the configuration file. Util. js and engine. js are built-in script files of DWR.

2) Compile JS functions that call Java Methods

For example, the following is a function call with no parameters and no return values.

Function callJava(){  Pict.method1();}

If no parameter exists, the return value is as follows:

Function calljava () {Pict. Method1 (retu); // retu is called a callback function. Actually, the return value is assigned to this function .} Function retu (data) {alert (data); // the data value here is the actual return value of Method1 .}

If there are parameters or return values, you can directly add them to the front of the callback function in the method.

Summary:

Through the example program above, we can see that using DWR to develop the B/S program brings us great convenience, but there are deficiencies in any technology, we also need to fully understand these shortcomings to help us better understand the applicability of DWR and give full play to its advantages.

For the framework study, if it is not dedicated to the development of the framework, we can not go deep into the study, stand on the shoulders of giants, and learn to use the wisdom of their predecessors.

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.