Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Boost Struts with XSLT and XML

An introduction to Model 2X

  • Print
  • Feedback

Page 5 of 6

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:

  • Print
  • Feedback

Resources