ToDoTasks: Use DWR to add AJAX to Java (1)

Source: Internet
Author: User
Tags xml example

It is not always difficult to make Internet-based Java applications more interactive, and Direct Web RemotingDWR) The Toolkit can help you implement most of the work in AJAX.

In the past, we used ToDoTasks software and added it to the work architecture, so that people can access the report through a browser. This month, we will continue to step forward, create a more interactive browser experience.

Now, we can compile our own servlet or JSP code to generate a more intelligent web page. This method works, however, the use of AJAX/DHTML in browsers for human-computer interaction is becoming more and more common, but the workload is also very large, and there are also potential difficulties in maintaining software, now we have a quicker method, which is the DWR we will use.

DWR stands for Direct Web Remoting working remotely on the Internet. The basic idea behind DWR is to make Java classes on servers visible to JavaScript code running in browsers.

DWR is based on the required Java class methods and fields. The JavaScript code created by DWR is also required to access these classes and fields. Therefore, you do not have to write your own AJAX code, here is a simple example:

package com.builder.uk.todotasks;

public class Adder {

public Adder() {}

public int add(int x,int y) { return x+y; }

}

Then, make the appropriate configuration at the appropriate location in an HTML file:

This will call DWR to generate a JavaScript interface for the Adder and include the DWR "engine" code and the DWR tool code. In the script on the page, we can use a call to access Adder:

Add. add (doneAdd, 100,200 );

The doneAdd in the script is a function reference. The function is called when a request is completed. The result of the call is Adder. add:

function doneAdd(result) {

alert(result);

}

In our example, "300" is displayed.

Before creating our new user interface, our first stop on ToDoTasks is to let it work, so we continue to use the source code in the previous article and add DWR. First, download DWR. DWR 2.0 is still under development, so we use the current version 1.1 to download the dwr. jar file. We add it to the previously created network application software folder instead of placing the jar file in the libs folder, So we create a lib folder in webapp/WEB-INF and. copy jar to this location.

Inside dwr. jar is the servlet and resource used by DWR. Therefore, we need to connect the DWR servlet with the network application software. Edit the webapp/WEB-INF/web. xml file and add the following statement:

dwr-invoker

DWR Servlet

uk.ltd.getahead.dwr.DWRServlet

debug

true

In the existing Define the DWR servlet after the entry and Add after entry:

dwr-invoker

/dwr/*

To map the DWR servlet to process all requests to/dwr. We used the code we showed earlier to create the Adder. java file in src/com/builder/uk/todotasks.

Now there is only one step. DWR is a folder named dwr through webapp/WEB-INF. xml file configuration, this file can control which Java classes are visible to JavaScript, the following is the dwr connecting to Adder. xml example:

"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"

"http://www.getahead.ltd.uk/dwr/dwr10.dtd">

In There is an element in the element that can control which classes are allowed to be accessed. Within this element, we have It defines when DWR creates an instance of the class. Here, the creator attribute is set to "new", which specifies that the 'new' operator of Java will be used when the instance is created. Other creator values include "scripted", which is used to create an instance using a scripting language, "spring", "jsf", "struts", and "pageflow" are used to create instances using these architectures, while "null" is used for static classes.

The Javascript attribute specifies the name used by the instance in JavaScript code. The scope attribute specifies the object's appearance and duration, the "session" is specified, so it will be created at the beginning of the browser session on the server.

An element can contain many elements, The element is transmitted to the creator, so in this case, we set the "class" parameter to the name of the Java class.

Now we can run the ToDoTasks program and open http: // localhost: 8080/dwr/in the browser. Here we can see the list of DWR classes, this page is the front-end for DWR testing/debugging. Open it in definition.

Figure 1

When we click the Adder link, we will get the Adder test page.

Figure 2

This first shows the code we need to add to a JavaScript page when using Adder, And then it lists the methods available for calling. This is a dynamic list, you can add () () enter a value in the Project Field and click execute to call the software/Server method.

Figure 3

This includes the basis of DWR. In order to study more deeply, we use a new version of ToDoTasks, which you can use to build and run. Like the Adder example, it has a DWR-based front-end interface that allows you to view, edit, and delete tasks.

Figure 4

To manage the front-end interface, we created a class named TasksSession. This includes the ArrayList of a Task instance, which can be sorted by a SortOrder variable. This variable can encapsulate this field for sorting, whether in ascending or descending order.


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.