Boost Struts with XSLT and XML

An introduction to Model 2X

1 2 Page 2
Page 2 of 2

XPath is more powerful than the Struts 1.0 expression language and thus is the language of choice for extracting data from the source XML stream. With XSLT constructs -- such as <xsl:for-each>, <xsl:if>, <xsl:choose>, and attribute value templates -- Struts HTML, logic, and bean tag libraries become obsolete. XSLT provides not only similar but enhanced functionality over the Struts tag libraries using languages standardized by the W3C (World Wide Web Consortium). In addition, XSLT is more powerful than just a few tag libraries; for example, it supports functions and recursion.

Other considerations

In this section, we explain how to address internationalization and error handling, as well as some current limitations and possible future improvements of Model 2X.

Internationalization

The localization level and the target language's complexity affect entire layouts as well as text messages and images. For instance, Arabic is written right to left, and Mandarin Chinese is written vertically, from top to bottom. Such languages not only require text translation but also a completely different Webpage layout. Struts developers usually save all text messages and image links in resource bundles with one bundle per locale. Depending on the user-chosen locale, the appropriate resource bundle is loaded.

Model 2X handles resources by automatically storing them in the DOM passed to the view. Reading resource bundles and creating a DOM can be cached for efficient access. Model 2X simply inserts the resources DOM into the final DOM tree, including the dynamic content generated from beans. You can then access resources easily with XPath in an XSLT stylesheet. You can switch stylesheets dynamically depending on the current locale, although in most cases resource-based localization proves sufficient.

Error handling

Validation errors need to be distinguished from more severe errors. Validation errors occur when input parameters are encountered in an HTML form. All other errors are considered severe.

In Struts, form validation in form beans can return ActionError objects. Those objects are stored in the request and then serialized into the DOM tree for use by the stylesheet, which can conveniently display them next to the form, for example.

Similarly, the user can manually identify errors in the Action and store them in the request for further serialization and handling in the stylesheet.

Depending on the type and content of the request's error object, the stylesheet can choose to display an error message or redisplay the previous page to let the user resubmit the form.

Workflow

In the Model 2X architecture, the struts-config.xml file does not control the workflow as it does in the original Struts architecture. This is actually more a limitation of this implementation than of Model 2X itself. You can fix this concern in subsequent implementations.

Output

Model 2X can dynamically change the output's content type and/or the user interface's style, an important feature of the architecture. For example, the same application can generate HTML 3.2 code for older browsers and HTML 4.0 code for newer ones. XSLT allows output in a variety of formats, including XHTML, XSL/FO, WML, simple text, CSV, PDF, SVG, and so on.

XSLT pipeline

As mentioned briefly above, Model 2X features a useful improvement: the ability to easily channel stylesheets, separating layout and styling logic. As is true with the Apache Cocoon Framework, one stylesheet can define, for example, how a particular table type displays across a whole site. The first stylesheet can output a table as follows:

  <xsl:template match="customer-info">
    <table>
      <tr>
        <td>Name</td>
        <td><xsl:value-of select="name"/></td>
      </tr>
    </table>
  </xsl:template>

The second stylesheet can style the table by creating an embedded table as follows:

  <xsl:template match="table">
    <table cellpadding="0" cellspacing="0" border="0" bgcolor="red">
      <tr>
       <td valign="top">
          <table cellpadding="4" cellspacing="1" border="0">
            <xsl:apply-templates select="tr"/>
          </table>
        </td>
      </tr>
    </table>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

Performance

Model 2X's current implementation dynamically creates an XML DOM tree and sends it to the XSLT transformer. Using SAX (Simple API for XML) notably enhances performance, particularly latency and large documents' memory footprints. In addition, stylesheet compilation improves XSLT processing times. Xalan, the Apache Project's XSLT processor, provides a mechanism to compile stylesheets into a set of Java classes, known as translets.

Client-side processing

The latest browsers, such as Internet Explorer 5/6 and Mozilla, can perform client-side transformations, which can relieve application servers' processing load. Similarly, XInclude (XML Inclusions), if supported, can download and cache resources and other data from the application server. XInclude provides a generic mechanism for recognizing and processing inclusions, and as such, can offer a simpler overall authoring experience, greater performance, and less code redundancy. This approach's drawback: developers must ensure that the XML documents and stylesheets sent to the clients contain only information the user is allowed to see.

Enhance Struts with Model 2X

In conclusion, XML and XSLT use in Model 2X encourages developers to separate business and presentation logic and brings the Web applications world a step closer to delivering on MVC's original promise. Additional benefits, such as well-formed output documents, reliance on standardized languages, increased presentation layer flexibility, and shortened development cycles make the new model not only elegant but also powerful and efficient.

You could easily improve the simple implementation described in this article to refine the model, tune performance, and add flexibility.

Julien Mercay is a senior software engineer at Orbeon, a company specializing in J2EE (Java 2 Platform, Enterprise Edition) and XML technologies. Before joining Orbeon as a Unix, Java, and XML guru, Mercay worked at Sun Microsystems where he received the Technical Achievement Award for his major contribution on Sun's Web-based auctioning system. Gilbert Bouzeid created several J2EE applications for big companies, including NEC Computer, Context Integration, and Symantec before joining Orbeon. He is well versed in e-commerce applications, particularly in databases and XML technologies.

Learn more about this topic

1 2 Page 2
Page 2 of 2