See how to use AJAX magic in Struts applications (1)

Source: Internet
Author: User
Tags ruby on rails

AJAX is a recent revolution in web development. It allows you to configure a large number of dynamic effects in a common web browser. Many years ago, Struts was already a de facto standard in Java-Web development and has been used in many applications. This article will show you how to combine multiple AJAX user interfaces in your existing Struts application?

What Is AJAX?

AJAX is short for "Asynchronous JavaScript and XML. This is a technology, not a framework like Struts. Why is there so much attention around AJAX? This is because AJAX makes web pages not look like a flat document, but more like dynamic GUI applications that users expect, such as desktop applications. AJAX technology can be used in many browsers, including IE and Netscape/Mozilla ). It is already used by Microsoft for Outlook web clients) and Google users' Google Maps and Gmail.

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 dynamically display a very slow and non-Enterprise Java method ); or you can submit it to the backend server without changing the form ). AJAX improves your ability to integrate the best solution: dynamic pages, but most applications are processed by Java programs on your web server.

Javascript 101

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 is not displayed as "blank" as usual ").

2. Content returned by XMLHttpRequest and Microsoft. XMLHTTP can be processed as XML or text. JavaScript on your page) You can use the new content of the request to update the page.

3. The entire processing process can be triggered by common JavaScript events such as onclick, onchange, and onblur.

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 for 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, such as 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, to 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 a value and click Submit


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.