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 4 of 6

Figure 3 shows an XSLT pipeline example. The following section presents another example of content and layout separation.

Transformation example

Consider a Struts application with a simple form bean containing two attributes:

  public class TestForm extends ActionForm {
      private String testString;
      private List testList;
  }


The XML serialization code produces the following XML fragment, assuming testString and testList are populated respectively with My Test String and One, Two, Three. Note that the element names in the XML document are predictable, making it easy to write the stylesheet:

  <page name="TestForm">
    <request>
      <TestForm>
        <testString>My Test String</testString>
        <testList>
          <item>One</item>
          <item>Two</item>
          <item>Three</item>
        </testList>
      </TestForm>
    </request>
  </page>


This simple XSLT template transforms the serialized stream to an XHTML fragment:

  <xsl:template match="page">
     <h2>Please enter some text and submit</h2>
     <br/>
     <form name="testForm" method="get" action="result">
       <input type="text" name="testString" 
value="{request/TestForm/testString}"/>
       <br/>
       <select name="outSelect">
          <xsl:for-each select="request/TestForm/testList/item">
            <option><xsl:value-of select="."/></option>
          </xsl:for-each>
       </select>
       <br/>
       <input type="submit" value="Submit"/>
     </form>
     <hr/>
   </xsl:template>


After transformation and HTML serialization, the result will look like this:

  <h2>Please enter some text and submit</h2>
  <br>
  <form name="testForm" method="get" action="result">
    <input type="text" name="testString" value="My Test String">
    <br>
    <select name="outSelect">
      <option>One</option>
      <option>Two</option>
      <option>Three</option>
    </select>
    <br>
    <input type="submit" value="Submit">
  </form>
  <hr>


Model 2X's major benefits

In this section, we explain the benefits that Model 2X offers compared to the earlier models.

Business and presentation logic separation

The raw material used by the stylesheet is the XML stream, which is entirely defined by the model and the context. Although some XSLT transformers support extensions, letting you call Java code or other languages from within a stylesheet, use of such extensions is limited due to the additional effort required and the resulting lack of portability. This difficulty effectively removes any possible side effects during the XSLT transformation and separates the business and presentation logic.

Use of standards

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.

  • Print
  • Feedback

Resources