Chapter 2. WebSphere Commerce Web 2.0 Store features and benefits . 17
2.2 Model View Controller design pattern in Web 2.0
2.2.2 How MVC applies for Web 2.0
View Controller Model
Action
Action
Action
Action Configuration
file ActionServlet
Model Tag libraries
Application Resources
: ActionForm : JSP
The Web 2.0 Store solution uses RIA technologies such as Ajax and Dojo widgets to provide customers with an interactive and rich shopping experience.
Web 2.0 Store solution
WebSphere Commerce is based on Struts Framework. All browser requests are sent to the Struts Action Servlet, which forwards the request to the corresponding action based on URls and the mappings from the configuration XML file. The Struts Action will perform a service and return a Struts Action Forward, which is used to render the new page to return to the browser.
The new component model requires that components define a set of services that accept Open Application Group Integration Specifications (OAGIS) Business Object Document (BOD) messages. This BOD messages architecture is passed to the component façades in the form of Java objects that are consistent with the structure of the request BOD message. The component façade methods will return OAGIS BOD responses. Web developers will need to access these component services through regular Web 1.0 page load requests, through Ajax style requests, as part of render requests, and on the server while a JSP is being rendered.
In WebSphere Commerce, the action services are handled by controller commands. This allows Struts actions to be configured using a new action implementation that will invoke the new component services. JSP authors will require component data to render the page. The Java Client API provides methods for Java programmers that allow them to retrieve static business objects with the required component data. JSP developers have access to tag libraries that allow them to call Get services methods and retrieve a list of business objects that match a specified XPath search expression. The populated business objects can be used by the JSP developers like regular data beans. To include Ajax-style requests in a JSP to render HTML pages for updating parts of a Web page, these requests have to call existing controller commands as well as new component services.
Figure 2-7 on page 34 illustrates an Ajax invocation pattern for an action that invokes a component service and then updates the page as required to reflect the new state.
Figure 2-7 Ajax pattern using component service
An Ajax-style request requires an invocation of a service that will do some update of the request, followed by an update of some parts of the Web page to reflect the updated information. For example, a user drags an item to the shopping cart.
This triggers an Ajax request to add the item to the shopping cart. When the item is added to the shopping cart, parts of the page will need to be refreshed to reflect the new state of the shopping cart.
The updated request will invoke a service that will perform the request. If the new component services are being used, then the request uses a new Ajax
component service implementation that invokes the component service. The Ajax component service action is configured to invoke a Web-friendly method on the component client façade that will accept a Map of name value pairs. The method will construct the BOD from the Map and invoke the appropriate Page
on the page that submitted the request. The response will contain standard name/value pairs that indicate if the request was successful or not. In the case of an unsuccessful request, a message is displayed to the user and the page is left otherwise unaffected. In the case of a successful update request, parts of the page may need to be refreshed to reflect the updated state.
When the update completes successfully, the affected parts of the page will launch Ajax requests that allow the page fragments to be refreshed. The Get Ajax request calls the Struts Servlet which will delegate the request to a JSP. The JSP is able to retrieve the appropriate data from the component service using the wcf:getData tag and render either HTML or JSON. The response format should be in the form that is easiest for the JavaScript code that is updating the page with the new data. Experience shows that we want to avoid delegating the complicated logic of parsing and formatting the new data to JavaScript running in the browser. It is much more efficient to handle this on the server side.
Figure 2-8 illustrates the typical Ajax invocation pattern for an action that invokes a controller command to perform a service, and then updates the page as required to reflect the outcome of the service.
Figure 2-8 Ajax Pattern using controller commands Page
The invocation pattern is the same as the pattern used when component services are used to perform the service.
When to use Ajax invocation pattern
The Ajax invocation pattern was derived from two design principles:
To ensure that the well-defined component services can be effectively used by the Web developer
Component developers are required to provide Web-friendly methods that can be used to invoke the services by passing name/value pairs rather than constructing the OAGIS BOD message.
To limit the complexity of the JavaScript required to update the page Experience with JavaScript in the tools framework has taught us that JavaScript does not scale when it is required to format and parse large amounts of data. By performing the complicated parsing and formatting using JSP technology (designed for that purpose), new page fragments can be returned to the browser in a preformatted response that is ready to be inserted into the page with a minimal amount of JavaScript.
In addition to the Ajax considerations, care has been taken to ensure that exploiting these new component services does not prevent Web developers from using the existing Struts actions and data beans. It is conceivable that a Web application may make use of both the new techniques for invoking new component services and the existing techniques for invoking Struts controller commands and data beans. It is possible that a single JSP may use the new get data tag library to get data from a new component service, and also use a data bean to retrieve from an existing Commerce component.
If the Web application is being rendered outside of the Commerce application, the Commerce Struts actions and data beans will not be available. For example, if the Web applications are part of a portal application, the developer will only be able to exploit Commerce through the component services.
Note: For implementation of the Ajax invocation pattern in the Web 2.0 Store solution, see “Scenario 3: Updating business objects in WebSphere
Commerce using Ajax, and returning all relevant update information through JSON” on page 133.