<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Flyingis

    Talking and thinking freely !
    Flying in the world of GIS !
    隨筆 - 156, 文章 - 16, 評(píng)論 - 589, 引用 - 0
    數(shù)據(jù)加載中……

    Sprinkle Some AJAX Magic in Your Struts Web Application(1)

    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.

    What is AJAX?

    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).

    Life Before AJAX

    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 101

    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:

    1. The 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).
    2. The content that the 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.
    3. The whole process can be triggered by the usual JavaScript events: onclick, onchange, onblur, etc.

    Using AJAX in Your Struts Application

    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?

    • Wait until the next version of Struts (Shale) incorporates AJAX techniques. For Struts developers starting a new application this is probably the best option. The downside is that this will probably require moving to JavaServer Faces--not a bad thing in itself, but this may entail fundamental changes if you have an existing application.
    • You could move to a new approach, like Direct Web Remoting (DWR) or Ruby on Rails, which are specifically built for AJAX applications. While these are both very impressive frameworks, and are worth taking a look at if you wish to consider web development without Struts, this option would mean rewriting your entire application.
    • Add AJAX to your existing Struts application. Since AJAX is a technique, not a framework, it is straightforward to add it to Struts. For existing applications where stability (e.g., keeping existing libraries) is important, this option is recommended and is the one we explore in more detail.

    Some other advantages of our preferred option are:

    1. It should not require any new libraries or server side code; only the Struts libraries and actions already in the application need be used.
    2. All of the parts of the solution--JavaScript, XML, Java and Struts--are already widely understood.
    3. The application can be migrated to AJAX piece by piece; we can identify those parts which will most benefit users, and then choose to upgrade them to dynamic AJAX behavior first.

    Implementing the Solution

    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:

    1. The user requests a screen by clicking a hyperlink or form submit button.
    2. The web server runs the requested Struts Action, generating the web page.
    3. The browser displays the page.
    4. When the user presses Save, the information is posted to the server, where it's converted by the Struts framework to an ActionForm class containing the posted data.
    5. The Struts framework then calls the Struts Action that then processes the request (e.g., saves the data to a database).
    6. The page is rendered as per item 2, and the process starts again.

    Existing Struts Application

    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.

    Non-AJAX sample: Initial screen
    Figure 1. Non-AJAX sample: Initial screen

    Non-AJAX sample: Values entered, Submit pressed
    Figure 2. Non-AJAX sample: Values entered, Submit pressed

    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:

    • The struts-config.xml file is set up to redirect all requests to http://localhost:8080/struts-non-ajax/ (or the equivalent in your web server) to index.jsp.
    • index.jsp contains a Struts form with two text boxes (showBlue and showGreen). The page also contains tags, but as the values for these text boxes are initially blank, the content within them is not displayed.
    • The user enters values (true or false) and presses the Submit button, passing control (via the Struts Framework, reading struts-config.xml) to the 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.
    • index.jsp now evaluates the request; if 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?

    posted on 2005-11-02 11:24 Flyingis 閱讀(869) 評(píng)論(0)  編輯  收藏 所屬分類: Recycle Bin

    主站蜘蛛池模板: 最近的中文字幕大全免费版| 亚洲综合激情视频| 国产亚洲漂亮白嫩美女在线| 1000部拍拍拍18免费网站| 亚洲免费精彩视频在线观看| 性xxxx视频播放免费| 久久青青草原亚洲av无码app| 久久精品无码免费不卡| 亚洲国产成人精品久久久国产成人一区二区三区综 | 精品日韩亚洲AV无码一区二区三区| 女人隐私秘视频黄www免费| 亚洲日韩精品A∨片无码| 国产精品网站在线观看免费传媒 | 7x7x7x免费在线观看| 亚洲国产超清无码专区| 91在线品视觉盛宴免费| 亚洲色一区二区三区四区| 国产老女人精品免费视频| 黄页网站在线视频免费| 亚洲熟女少妇一区二区| 久久国产乱子伦精品免费一| 亚洲精品在线免费观看视频| 好先生在线观看免费播放| 日本亚洲中午字幕乱码| 国产精品V亚洲精品V日韩精品 | 一级中文字幕免费乱码专区| 亚洲精品国产精品乱码不99| 91精品成人免费国产片| 亚洲欧美日韩久久精品| 亚洲伊人成无码综合网| 免费无码VA一区二区三区| 亚洲人成欧美中文字幕| 亚洲一级Av无码毛片久久精品 | 午夜一级毛片免费视频| 免费无码婬片aaa直播表情| 亚洲成av人片天堂网| 一个人看www在线高清免费看| 男人扒开添女人下部免费视频| 亚洲成A人片在线观看无码不卡| 免费在线看v网址| 一级毛片大全免费播放下载|