
| Create Ajax-style architectures with the IBM Web 2.0 Feature Pack | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 摘自: IBM developerWorks Worldwide 被阅读次数: 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
由 yangyi 于 2008-03-24 21:55:27 提供 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Level: Introductory Kevin Haverlock (kbh@us.ibm.com), Software Developer, IBM 18 Mar 2008 This article shows you how a Java™ 2 Platform, Enterprise Edition (J2EE) application was enhanced with an Ajax-style architecture by using the IBM® WebSphere® Application Server Feature Pack for Web 2.0. Learn how to combine Ajax-style architectures with an existing application without having to rewrite the entire Web application. You'll also discover some ideas on how to apply the Web 2.0 Feature Pack to your own J2EE applications for IBM WebSphere Application Server. The "Plants by WebSphere" application is among a number of samples that are provided with the IBM WebSphere Application Server Feature Pack for Web 2.0. The application exemplifies a typical J2EE application and how it can be enhanced with an Ajax-style architecture without rewriting the entire application. The sample application represents a fictitious online plant store where customers can order and purchase flowers, trees, vegetables, and accessories. Take a look at Figure 1, which shows the front page of the Web application: Figure 1: The Plants by WebSphere Web application
Figure 2 illustrates the architecture of the application in its original form before attempting to add Ajax-style features. The architecture is intended to be fairly typical for a J2EE application running on WebSphere Application Server. At a high level, the application adheres to a Model-View-Controller (MVC) design pattern, which most Web applications follow on some level. A browser accesses the URL for the application, which returns a JSP-rendered HTML page. The browser issues additional requests to the Web application, and servlets are used to control the flow as users move through the purchase request. Enterprise JavaBeans (EJBs) are used to serve model data available on the database. Figure 2: A typical Web architecture
Figure 3, below, shows how the architecture of the application has been augmented using Ajax. The intention is not to rewrite the application, but rather to take advantage of technologies in the IBM Feature Pack that would improve and create a more interactive and rich experience for the user. On the browser side, the application uses widgets provided with the JavaScript Dojo Toolkit. Additionally, customized user interface widgets were created to improve the interactive nature of Plants by WebSphere while not rewriting it. The customized user interface widgets are asynchronous, meaning they communicate using the browser's XHR mechanism supported by the Dojo Toolkit. The widgets use an XML interchange format to exchange data with the server. On the server, the RPCAdapter provided with the Feature Pack is used to convert EJB data into an XML interchange format that can easily be consumed by your newly created widgets on the browser. A characteristic of Ajax applications is an improved response on the user interface.
Plants by WebSphere uses the Dojo Toolkit within the Web browser to improve the user
interface (UI) of the application. The Dojo Toolkit is pure JavaScript, and the JavaScript
files can be hosted directly in the The Dojo Toolkit supports being used declaratively or procedurally. In a declarative role, you embed the JavaScript you plan to use directly within the HTML content. The Dojo Toolkit contains a wealth of widgets that can be used declaratively within HTML, reducing the need for you to hand code the function. In the case of Plants by WebSphere, Dojo Toolkit widgets are directly embedded into the JSP pages. Figure 3: An Ajax-enhanced architecture
A common scenario in Web applications is form handling, where the user enters data into a Web page, data such as his or her name, address, preferences, and so on. The information is then sent back to the server for processing, and the result is returned to the user. A common action on the form is validation to ensure the content the user is entering is in an accepted format. Was a number entered in place of a letter? Did the user enter a valid zip code? The Dojo Toolkit provides a rich set of form-handling validation capabilities that can be added to Web pages. Listing 1, below, is an example that is used within the Plants by WebSphere application. It represents a typical example of how to declaratively use the Dojo Toolkit within a Web application. We'll begin by declaring the usage of Dojo within your HTML page. Looking at Listing 1, the
first script tag declares usage of Listing 1: Sample code listing at maximum width
Listing 1 showed how the Dojo Toolkit is declared within your JavaScript and the type of
widgets the page will use. Listing 2 shows how the widget is declared within the page.
The Listing 2: Declaring the use of Dojo's ValidationTextBox widget
Figure 4 shows the result. When the user fails to enter a value for a required field, the browser will dynamically inform the user. Figure 4: Billing address using Dojo's validation widget
Form validation is a simple example of how to begin to use Ajax within an existing J2EE application. At the same time, it represents a powerful example of how seamlessly a user interface can be improved without having to rewrite a ton of existing code. There are numerous other examples of how declarative forms of programming using the Dojo Toolkit can be used within a J2EE application. Dojo ships a wide array of sample applications within the test directories of the Dojo Toolkit. These applications will run within a browser by opening the HTML page.
Creating your own custom widgets Another interesting example is creating your own customized JavaScript widgets. Dojo provides a powerful framework to create innovative widgets that can be embedded directly within your HTML pages. Figure 5 shows the catalog browsing page for the Plants by WebSphere application. When the user clicks one of the images, the catalog page shows pictures of items across the top and detailed information of each item at the bottom. Figure 5: Catalog page for Plants by WebSphere
While this page consists of a number of different widgets, we'll look at the Figure 6: The itemDetails widget
When creating widgets using the Dojo Toolkit, there are several files you must consider. The first two are the HTML and
CSS template files. These will be used to define the skeleton of what the page will look like when the
widget is rendered within the browser. The HTML page will contain declared attachment points where the
DOM elements will be inserted by your widget. The attachment points are called Listing 3 shows a portion of the Listing 3: DojoAttachPoint in the itemDetail.html Dojo template
The widget's template and CSS define the appearance of the widget and identify where in the DOM updates will occur. The next step is to add the JavaScript code to the widget that does the work. Listing 4 shows the beginning section of the customized Below the Further down in the code, the Listing 4: Declaring the itemDetails widget
To really make the widget interesting, data is needed to populate it. The data for the details widget is located on the server. At this point, you might wonder how to get the description data to render in the page. The Dojo Toolkit offers a powerful I/O transport mechanism that can be used to issue XMLHttpRequests (XHR) requests to the server and process the result. The XHR API opens an independent communication channel within the browser. XHR is at the heart of what gives Ajax its interactive feel. Listing 5 shows the use of the The Listing 5: XHR GET request to the server
When your customized Dojo widget is created, it needs to be inserted into your HTML page so it can be
used. Figure 6 shows what the page looks like. The listing is from the flowers.html file. Listing 6
shows a fragment of the HTML behind the page. The Listing 6: Declaring itemDetails in the HTML page
At this point, you have seen how a customized widget is created in Dojo and how that widget would go about issuing XHR requests to the server. Now look at how the server might handle those requests. In the case of the Plants by WebSphere application, the widget issues an XHR request to the server for some data. The servlet reads the request and looks up the correct Enterprise JavaBean (EJB). The EJB is called, and the EJB container issues a request to the database, which returns the result. The result needs to be encoded into XML and returned back to the JavaScript widget on the browser. For this scenario, the RPC adapter (RPCAdapter) that is provided with the IBM Feature Pack for WebSphere Application Server will be used to connect to the Plants by WebSphere EJBs. The RPCAdapter runtime JAR is included with the Plants by WebSphere application and is configured using an XML file located in the WEB-INF directory of the WAR file. Listing 7 shows an example configuration. Listing 7: RpcAdapterConfig.xml
Take a closer look at the RpcAdapterConfig.xml file. The Putting it all together, the
The result returned by the RPCAdapter would be XML data. Listing 8 shows the XML output: Listing 8: XML output returned to the itemDetails widget from the RPCAdapter
Now take a closer look at the implementation class. This was defined in the RpcAdapterConfig.xml
file and contains the Listing 9: Implementation class com.ibm.websphere.samples.plantsbywebspherewar.RpcConnecter
The method By using the RPCAdapter and having it return XML code, you have created a service that others can connect to as well. The service only returns data, and it is up to the caller to figure out how to render the data. A usage scenario might be a partner company of Plants by WebSphere that aggregates data from its own greenhouse with the catalog information maintained by Plants by WebSphere. This scenario is sometimes referred to as a mashup.
Other scenarios to think about While the Plants by WebSphere application does not implement the specific scenarios listed below, they are helpful in understanding other ways to use the benefits of the IBM WebSphere Application Server Feature Pack for Web 2.0 in your J2EE applications. You have just learned about creating a mashup application that combines content from multiple sites to create the appearance of one site. The classic mashup example is taking Google maps and customizing it with a user's unique location-based content. One of the challenges in creating mashups is dealing with cross-site scripting within the browser. As an example, if you go to mydomain.com to access the Plants by WebSphere application, but the widgets you created issue XML requests to mypartner.com using XHR, then the browser will prevent the request. Normally, this is a very good behavior because it prevents cross-site scripting security vulnerabilities from occurring when you access pages on the Web. In the case of the Plants by WebSphere application, you want to allow your widgets to access other sites for content. How do you allow cross-site scripting responsibly within the browser? One common way is to use a forward proxy. A forward proxy takes the request from the browser, looks at the URL, and forwards the request to the appropriate domain. From the browser's perspective, the information appears to come from the same domain, but in reality, the content request is made by the proxy on behalf of the client. The Ajax proxy that is shipped with the IBM WebSphere Application Server Feature Pack for Web 2.0 includes a customizable servlet proxy. The proxy is servlet based, and can be embedded directly in an EAR file or run on your server as a WAR file. The proxy also includes a white-listing ability, which you can further use to restrict the kinds of requests that the proxy is allowed to service. It also includes the ability to filter on headers, mime types, and cookies, and it's customizable using an XML file. Web feed syndication -- tell the world Web syndication (or Web feeds) is the practice of making content available to other sites to use. Typically, a site makes available a Web feed that contains a title and a short description of the content. If a user is interested in the content, he or she clicks on the link and is taken to the site to read more detailed information. In the case of the Plants by WebSphere application, you can imagine a scenario where a Web feed would contain additional planting tips, sales, or blogs by horticulture specialists.
The IBM Feature Pack for Web 2.0 provides the Apache Abdera library which includes an implementation of the Atom Syndication Format and Atom Publishing protocol to help you develop your own feeds. A syndication solution would not be complete without taking into account the client-side implementation. The IBM Feature Pack provides an Atom Feed viewer as part of the Dojo Toolkit extensions. The viewer can be embedded in your HTML as a widget to provide feed-viewing capabilities.
The Plants by WebSphere application is just one example of how you can apply the IBM Feature Pack for Web 2.0 to create Ajax-style architectures for existing J2EE applications. The Feature Pack ships with a variety of client-side and server-side technologies that you can deploy together or independently with your Web application. Additional information is available with the Feature Pack download (see the Resources section for information). Learn
Get products and technologies
Discuss
Original link: http://www.ibm.com/developerwork... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
