Designing the View in Jakarta Struts
If you want your application's users to see your beautiful work, you need to create a visual component for your application. In the Struts implementation of the MVC pattern, you have complete flexibility to choose the View technology of your choice. After you choose a technology, you need to implement it. Here are some View options, how to create applications for an international clientele, and how to connect the View to the Controller.
Struts doesn't care what View architecture you choose to use. Most developers use the JSP architecture, but this is not a requirement. However, Struts does distribute a comprehensive tag library that you can use with JSP pages to make writing the JSP pages easier.
Other view creation possibilities may better fit your needs. The following sections explain a sampling of your options.
Template engines are characterized by a separation of the page design from page data. This methodology offers several advantages over plain vanilla JSP, such as
- More flexibility in site design
- Easier design development for graphics people
- Greater control of the consistency of the site appearance.
Following are examples of three template engines.
Apache Cocoon and the Cocoon plug-in
The Apache Cocoon project is a Web application framework built on the premise of separating concerns — that is, separating presentation from logic from data. Apache Cocoon contains pipelines (a particular path for the flow of transformations) that create XML data from various sources and then transform that data into various presentation technologies through the use of XSL (eXtensible Stylesheet Language) stylesheets. Cocoon offers a wide range of possible transformations, including JSP, Velocity, FreeMarker, PHP, and XSP. For more information see the Cocoon Web site.
The Cocoon plug-in allows Struts to pass forwards to Cocoon for transformation in one of Cocoon's pipelines. (A forward, more exactly known as an ActionForward, is a mechanism that defines the passing of control to another resource, usually a JSP page or a servlet.) See the Struts Applications Web site for more information.
Jakarta Velocity and VelocityStruts
Velocity is a Jakarta project — a Java-based template engine that provides a simple scripting language to create pages. No Java code is allowed in the pages.
VelocityStruts is an extension to Struts that seamlessly marries the Struts Framework to Velocity. With the VelocityStruts extension, developers can forward a request to a Velocity template instead of to a JSP page. The nice thing about this method is that you are not forced to choose between one technology or the other. You can mix and match as you see fit. To find out more about VelocityStruts, see the Apache Jakarta Project.
FreeMarker generates text output (anything from HTML to PDF files) based on templates. The FreeMarker templates are essentially page designs that contain no application logic, only page design information. This provides a clean separation of concerns between page designers and application programmers. The framework works with Struts out-of-the-box and replaces the use of JSP and JSP tag libraries as presentation technologies.
FreeMarker is an open source project. Further information on FreeMarker can be found at the FreeMarket Web site.
The advantage of an XML document is that you can use XSL stylesheets to transform the document into virtually any other type of document for presentation. This transformation process is advantageous for sites that need to offer many forms of display to the user. For example, you may want to let the user view a purchase order in HTML, PDF, or plain text. This section describes two tools that integrate with Struts to provide XML and XSL services.
The StrutsCX framework replaces JSP with XSLT (eXtensible Stylesheet Language Transformations). StrutsCX outputs well-formed XML that can then be transformed into any number of presentation markup languages (HTML, CSV, PDF, WML, and so on) using XSL stylesheets. See this Web site for more information on StrutsCX.
The four letters stxx are an acronym for Struts for Transforming XML with XSL. The stxx technology bills itself as an extension to the Struts framework that allows an action to return an XML document that will be transformed into the final presentation form by XSL or Velocity. The purpose of this system is to provide an alternative presentation technology to JSP. However, you can still use JSP alongside stxx. Take a look at SourceForge to find out more.