Use Ajax in Struts applications (1)

Source: Internet
Author: User

Before AJAX is used

Currently, most Struts applications use the standard "web page like a flat document" structure. If you want to imitate some desktop applications (such as those created using Java Swing, Visual Basic, or Delphi), you have two options: you can send all the information that may be requested as part of the page, and use a large amount of JavaScript to operate on its dynamic display (a very slow and non-enterprise-level Java method ); or you can submit it to the backend server without changing the form (an effective method ). AJAX improves your ability to integrate the best solution: dynamic pages, but most applications are processed by Java programs on your web server.

AJAX

AJAX is very similar to the existing dynamic HTML technology, and adds a request sent to the "backend" server to obtain the required new or updated information. The AJAX mechanism has been described in detail elsewhere-please refer to the Resources after this article for more information. But at least you need to know:

  • 1. XMLHttpRequest (if you are using the IE browser, It is the ActiveX object of Microsoft. XMLHTTP ). You can use JavaScript to call these objects on a web page. They allow you to request the content of the web server as a backend call method (for example, after a form is submitted, the screen does not display "blank" as usual ").
  • 2. Content returned by XMLHttpRequest and Microsoft. XMLHTTP can be processed as XML or text. JavaScript (on your page) can use the new content of the request to update the page.
  • 3. The entire processing process can be triggered by common JavaScript events: onclick, onchange, onblur, and so on.

Use AJAX in your Struts Application

After reading this article, you will be interested in using AJAX to create dynamic web pages and want to know how to add them to your Struts application. This is only one of the options. So how do you choose?

  • • Wait, knowing that the next version of Struts integrates AJAX technology. If the Struts developer is preparing to develop a new application, this may be a good choice. Later, JSF may be required to do the same-it is not a bad thing for itself, but it will bring the most fundamental changes to existing systems.
  • • You can also directly adopt new methods, such as Direct Web Remoting (DWR) and Ruby on Rails, which are designed to build AJAX applications. If you do not consider using Struts for web development, these are all very useful frameworks and worth a try. However, this also means that you must rewrite your application.
  • • Add AJAX to your existing Struts application. Since AJAX is only a technology rather than a framework, it is easy to integrate into Struts. As an existing system, its stability (for example, maintaining existing library files) is very important. Therefore, this method is recommended and will be detailed later.

The advantages of adding AJAX to Struts applications are as follows:

  • 1. It does not need any new library file or server code; it only needs to use the existing Struts library file and action.
  • 2. All parts of the solution-JavaScript, XML, Java, and Struts-are already widely known.
  • 3. This application can be transplanted to AJAX one by one. We can determine which parts are useful to users and update them to dynamic AJAX display first.

Implementation Scheme

How can we truly implement our choices? First, we should pay attention to how a "standard" (without AJAX) Struts application works. In this application, a general event process is as follows:

  • 1. Click the hyperlink or form submission button to send a request.
  • 2. The web server runs the Struts Action to process the request to generate a web page.
  • 3. The web page is displayed in the browser.
  • 4. When you click Save, the information is converted and sent to the server by an ActionForm class in the Struts framework.
  • 5. Then, the Struts Framework calls the Struts Action to send a request (for example, save the data to the database ).
  • 6. The page is returned again, and the process continues.

Existing Struts applications

A simple Struts application that demonstrates the event flow can be downloaded at the address below: struts-non-ajax.zip. This Struts-based application displays or hides blue and green tables based on user input. Figure 1 shows the screen for loading the initial Page. Figure 2 shows the user input value and click Submit. Although simple, it is enough to represent a Struts workflow.

Figure 1. Example without AJAX: initial Screen

Figure 2. Example without AJAX: Enter the value and click Submit

The server-side code is: A Struts Action forwards to (identical) JSP using the values defined in the struts-config.xml. Note the following points in the code in this example:

  • • The struts-config.xml file redirects all requests to index. jsp at http: // localhost: 8080/struts-non-ajax/(or the same as your own server.
  • • Index. jsp contains a Struts form (showBlue and showGreen) with two text boxes ). This page also contains tags, but the content between tags is not displayed, just as the two text boxes are initialized to be empty.
  • • The user inputs a value (true or false) and click the submit button to submit the processing control (read the struts-config.xml through the Struts Framework) to the SampleAction class.
  • • SampleAction records the values and forwards them to index. jsp. A mature Struts application may deal with more things. It is better to save or query databases.
  • • Index. jsp now reprocesses the request. If the value of ShowBlue or ShowGreen is true, these tables are displayed.

The application does not have any "errors ". Similar Struts projects have been doing this for many years. However, how can we add dynamic elements to this application without adding complex JavaScript or frequent form submissions?


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.