Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Generate Web Output in Multiple Formats and Languages with StrutsCX : Page 3

StrutsCX overcomes the limitations of the Struts Framework by enabling you to utilize XML, XSLT, and XPath technologies instead of its standard JavaServer Pages.


advertisement
The StrutsCXServlet
As Figure 5 shows, in StrutsCX, the XSLT transformation process is organized through the StrutsCXServlet and its helper classes, StrutsCXDocumentBuilder and StrutsCXTransformer. The StrutsCXServlet controls transformations by:
  1. Creating an XML document with the help of the StrutsCXDocumentBuilder
  2. Forwarding this document to the StrutsCXTransformer for transformation purposes

Figure 5: The StrutsCXServlet

The StrutsCXDocumentBuilder gets the information it needs to generate the XML-Output-Document through the HttpServletRequest, the HttpSession, and the ServletContext. All of these are handed over to the one and only public method, createDocument(). The Struts Action checks for the wanted information in the HttpServletRequest, the HttpSession, or the ServletContext, depending on which is best suited to keep the information. The virtual XML-Output-Document that the StrutsCXDocumentBuilder creates contains the following information:

  • The data of the integration tier—this could be database data, data from Enterprise JavaBeans, or data directly out of flat XML files
  • An (optional) ActionForm Bean with the state of the client
  • An (optional) ActionErrors object with the Struts error messages
  • The StrutsCX-Resource-Properties, which under StrutsCX replace the Java Resource Bundles
  • The data of the HttpServletRequest
  • The data of the HttpSession


  • To create the XML-Output-Document use JDOM.

    In StrutsCX, the values of each dataset can be presented to the StrutsCXServlet as an XML object or as a JavaBean. If you choose the latter, you need to add one or more JavaBeans to an ArrayList object, which is then passed to the StrutsCXDocumentBuilder via the HttpServletRequest. The StrutsCXDocumentBuilder uses its helper class StrutsCXBeanToElement to automatically create XML Elements out of the JavaBean's data and add it to the XML-Output-Document (see Figure 6).

    Figure 6: The XML-Output-Document

    This helper class is based loosely on its archetype, Model 2X, a project quite similar to StrutsCX that Julien Mercay and Gilbert Bouzeid developed. It analyzes the content of each JavaBean with the help of the Java Reflection API and the Java BeanInfo interface. It generates the XML Element out of the name and value of the JavaBean setter and getter methods.

    In much the same way, StrutsCX serializes data out of the ActionForm object with its setters and getters, the HttpServletRequest, the HttpSession, as well as the ActionErrors object with its error messages. I'll discuss the StrutsCX error handling a little later.

    The StrutsCX-Resources-Properties are XML files that are analogous to the Struts ApplicationResourses.properties. You should provide one for each language in which you want to present your application. It should contain all the internationalized text strings you need in your presentation, besides the data of the integration tier. Other than the ActionForm and the ActionErrors, the StrutsCX-Resources-Properties should not be transferred to the StrutsCXServlet via the HttpServletRequest. Instead, use the ServletContext just once at the start of the Servlet Container, right after the first call of one of the Action classes. This ensures better performance.

    The StrutsCXTransformer does the actual XSL transformation. This class encapsulates the whole transformation process by using JAXP, the Java API for XML Parsing—which is incredibly cool because it abstracts from the de facto XSLT processor you use. Of course you have to provide a proper XSL Stylesheet too, which should likewise be selected by the Action class and added to the HttpServletRequest. The StrutsCXTransformer reads and parses this XSL Stylesheet just in case it was changed since the last call. It uses its helper class StrutsCXURIResolver, which automatically converts relative URIs into absolute URIs.

    The StrutsCXTransformer also offers some practical goodies to make your life easier. For instance, if you'd like to send the XML-Output-Document directly to the HttpServletResponse, add the following parameter to the HttpServletRequest:

    debugxml=true

    This feature is very convenient for the development of XSL Stylesheets and looks great in the newest versions of Internet Explorer and Netscape/Mozilla, in which you can open and close whole XML Element sets.

    If you'd like to disburden your Web server and have the client do the XSLT transformation, you can set another parameter to the HttpServletRequest:

    xsl_clientside=true

    The StrutsCXDocumentBuilder adds a Processing Instruction to the XML-Output-Document with a pointer to the location of your XSL Stylesheet. This document is sent directly to the client's browser. No server-side transformation takes place.

    As mentioned before, StrutsCX rejects the Java Resource Bundle technique and uses the StrutsCX-Resource-Properties, simple XML files to save text strings for internationalized Web applications. Similar to Struts, StrutsCX can select the appropriate file during runtime by checking the java.lang.Locale object. StrutsCX-Resource-Properties result in quite a lot advantages for you:

  • Besides using the Java Resource Bundle, you can check the consistency of your properties file by providing a DTD or an XML Schema.
  • Like always in XML, you can organize the content hierarchically:

    <variables lang="de"> <strings> ... <day>Tag</day> ... </strings> ... </variables>

  • Inside the XSL Stylesheets, you can easily gain access to these values by using W3C standard XSLT/XPath expression instead of a JSP-Tag:

    <xsl:value-of select="//variables/strings/day"/>

  • XML uses UTF-8 as standard encoding and offers access to letters in all languages. The StrutsCX-Resources-Properties benefits from this.
  • The StrutsCX Resources-Properties can be filed outside the classpath. Like all the files you like to hide from direct access via HTTP, it's best to put them somewhere inside the /WEB-INF folder.
  • Translators can use modern XML Editors to edit comfortably the content of the XML files (see Figure 7). There is no similar feature when editing the Java Resource Bundles.
  • In StrutsCX all configuration files are XML based. There is no mix of Java properties and XML properties like you have in Struts.
  • Figure 7: Simple Editing of the StrutsCX Resources-Properties

    Note that the StrutsCX-Resources-Properties should be read into the ServletContext as an org.jdom.Document object the first time one of your Action classes is called. StrutsCX learns where to find them from the strutscx-config.xml, the special StrutsCX configuration file. Therefore inside the web.xml, the initialization parameter to read in the Resource Bundles is obsolete.



    Comment and Contribute

     

     

     

     

     


    (Maximum characters: 1200). You have 1200 characters left.

     

     

    Sitemap