AJAX is the latest revolution in web development circles, allowing rich dynamic interfaces deployed within a normal web browser. Struts has been one of the de facto standards for Java-Web development for a number of years, with a large number of applications already deployed. This article will show you how to combine the richness of an AJAX user interface with your existing Struts applications.
This article shows a simple and elegant way to do this by including a couple of lines of JavaScript on your JavaServer Pages (JSPs). While we show how to reuse existing Struts actions, the techniques are equally applicable to the Java-Web framework of your choice. The method proposed will also allow a move to the next version of Struts (Shale) or JavaServer Faces (JSF) in the future.
AJAX stands for "Asynchronous JavaScript and XML." It is a technique, rather than a framework (such as Struts). The reason for the buzz around it is that it allows web pages to behave less like flat documents and more like dynamic GUI apps that users might expect from their desktop environments. AJAX techniques can be used for all recent browsers (including Internet Explorer and Netscape/Mozilla). It is already used by (among others) Microsoft (for its Outlook web client) and Google (for Google Maps and Gmail).
Most current Struts applications follow the standard "web page as a flat document" structure. If you wanted to mimic the behavior of GUI desktop apps (such as those built using Java Swing, Visual Basic, or Delphi) you had two choices: you could either send all of the information that might possibly required as part the web page with (a lot of) JavaScript to handle the dynamic behavior (a slow and not very enterprise-Java way to do things), or you could do constant form submits back to the server (an effective, if somewhat clunky, method). AJAX gives you the best of both worlds: dynamic web pages, but with most of the application running in Java on your web server.
AJAX is similar to existing Dynamic HTML techniques, with the addition of a "background" call to the server to get new/updated information as required. The mechanics of AJAX have already been covered in detail elsewhere--take a look at the Resources section at the end of this article for some good examples. The minimum you need to know is:
XMLHttpRequest
(or Microsoft.XMLHTTP
ActiveX object if you are using Internet Explorer). These objects can be called from the JavaScript on your web page. They allow you to request content from your web server as a background call (i.e., the screen does not "go blank" as usually happens during a form submit). XMLHttpRequest
and Microsoft.XMLHTTP
objects return can be treated as either XML or plain text. JavaScript (on your web page) can then update the page with this new content as required. onclick
, onchange
, onblur
, etc. The chances are that if you are reading this article, then you are interested in AJAX‘s ability to create dynamic web interfaces and would like to know how to add it to a Struts application. What are your options if you want to do this?
Some other advantages of our preferred option are:
How do we actually implement our chosen solution? We start by reminding ourselves how a "standard" (non-AJAX) Struts application works. In this application, the normal flow of events is as follows:
Action
, generating the web page. ActionForm
class containing the posted data. Action
that then processes the request (e.g., saves the data to a database). A simple Struts application demonstrating this flow of events can be downloaded here: struts-non-ajax.zip. This application, based on the sample applications provided with Struts, either hides or displays blue and green tables depending on the values entered by the user. Figure 1 shows the screen on initial page load. Figure 2 shows the screen after the user has entered values and pressed Submit. Although simple, it is enough to demonstrate a Struts application at work.
The server-side code is as you would expect: a Struts Action
that forwards to the (same) JSP using the values defined in struts-config.xml. Some other points to note in this code sample are:
http://localhost:8080/struts-non-ajax/
(or the equivalent in your web server) to index.jsp. showBlue
and showGreen
). The page also contains <logic:equal>
tags, but as the values for these text boxes are initially blank, the content within them is not displayed. SampleAction
class. SampleAction
logs the values, and then forwards back to index.jsp. A more sophisticated Struts application would do more, such as saving to or retrieving from a database. showBlue
or showGreen
are true
, the tables will be displayed. There is nothing "wrong" with this application. After all, similar Struts projects have been deployed for years. But how do we to add dynamic behavior to this application, without adding complex JavaScript or continual form submits?
聯(lián)系客服