Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Boost Struts with XSLT and XML

An introduction to Model 2X

Since the Java Servlet API's inception, developers have used diverse technologies to develop Web applications in Java. Recently, developers have started to recognize the merits of Model 2, a scheme derived from the Model-View-Controller (MVC) paradigm. Model 2's benefits include an improved separation of the application logic and presentation layers. Struts, built on top of Model 2, additionally offers Java developers a generic controller servlet, centralized configuration, resource management, and error handling.

In this article, we introduce Model 2X, a scheme that further enhances Struts. By replacing JSPs (JavaServer Pages) with XML and XSLT (Extensible Stylesheet Language Transformations), Model 2X offers an even better approach to separating logic and presentation. We start with an introduction to Model 1 and Model 2, describe how Struts implements MVC, and finally show how XML and XSLT can be used to improve the existing models.

Model 1

Understanding Model 1 is crucial to understanding the subsequent architectures we discuss in this article. Model 1's cornerstone is the JSP file, which extracts parameters from an HTTP request, calls the required business logic, handles the HTTP session, and then generates HTML.

A complete Model 1 application is composed of a collection of JSP files, mostly independent of each other, and, optionally, a collection of support classes and other components. Earlier technologies, such as ASPs (Active Server Pages) and PHP (hypertext preprocessor), used this same model.

Model 1's major, and possibly only, benefit: simplicity. However, Model 1 encourages the developer to mix business logic and presentation logic, a significant drawback. While this model is appropriate for creating small applications, complex application development with Model 1 can become difficult to manage.

Model 2, MVC, and Struts/JSP

Object-oriented paradigms in the form of the MVC architecture rescued the JSP and servlet world by defining Model 2. Figure 1 illustrates MVC's three parts and their implementation in Struts/JSP.

Figure 1. Model 2

Controller

As Figure 1illustrates, the Struts' main component is a generic controller servlet. The Struts controller provides the initial entry point for all HTTP requests routed to Struts. It interprets and dispatches all requests to individual actions implemented by the developer as subclasses of the Struts Action class. It also automatically fills out form beans from request parameters. Individual actions implement core business logic by, for example, making EJB (Enterprise JavaBean) calls, and access the model through JavaBeans. An XML file that maps request URIs to actions and form classes configures the controller servlet.

Model

The model takes the form of one or several JavaBeans. Those beans fall into three categories:

  • Form beans hold either the data the user entered in HTML forms or, more generally, any attribute passed either on the URL or in a POST. For instance, a form bean for a login page could have two properties: login and password. Form beans extend the Struts ActionForm class.
  • Request beans hold information needed to generate the HTML page. In the case of a bank account statement page, a request bean would have properties with information about the account as well as a transaction bean collection detailing the most recent transactions to display.
  • Session beans hold session information that persists between two HTTP requests by the same user.


View

The controller servlet forwards a request to a JSP implementing the view. The JSP can access the form, request, and session beans and outputs a result document (usually an HTML document) sent to the client. Struts provides four JSP tag libraries:

  • HTML: Helps create HTML tags, particularly by filling out HTML forms with data from the model.
  • Bean: Manipulates beans.
  • Logic: Implements logic constructs based on bean values.
  • Template: Handles page templates.


With the Struts tag libraries, you can usually avoid using any Java code in the view.

Struts/JSP drawbacks

While the Struts/JSP approach features many advantages over other models, it exhibits several drawbacks:

  • As in Model 1's case, nothing prevents a developer from embedding application logic into the JSP. Experience shows that many developers actually fall into that trap, often to perform a quick fix. This can lead to applications that are hard to understand and maintain.
  • The JSP syntax is not XML compliant and therefore fails to guarantee that resulting XML or HTML documents will be well formed.
  • The developer must learn new APIs -- the Struts tag libraries. Experience shows that gaining an understanding of the Struts tag libraries, particularly the bean and HTML libraries, can take a long time.
  • You can't implement a real processing pipeline in the view with JSP. Only basic includes and forwards are possible, effectively limiting the view's flexibility. For example, separating layout and style proves difficult.
  • With JSPs, you must recompile after every change, which can be time consuming. Just ask any JSP developer waiting for page compilation after every mouse click.


The solution to these problems must:

  • Restrict the visibility in the view to the model and some well-defined context information, such as resources.
  • Enforce well-formed XML and HTML.
  • Leverage existing languages or APIs.
  • Ease the separation of different view aspects, like layout and style.
  • Allow for a faster development cycle.


As detailed, the Model 2 technology currently features many issues that must be addressed. We believe the lightweight framework detailed below, based on an unmodified version of Struts and XSLT, exceeds the given requirements. We call the new architecture Model 2X.

Model 2X: Architecture overview

Model 2X is the symbiosis between Struts and XSLT. Model 2X uses the Struts model (its controller servlet), but the view implementation uses XSLT and beans serialized into XML instead of JSPs.

What is XSLT?

XSLT is a functional language designed to perform transformations on XML documents. It is part of a stylesheet language for XML known as XSL. XSLT uses XPath, an expression language that accesses or refers to parts of an XML document. In addition to XML dialects such as XHTML, XSL/FO (formatting objects), or SVG (Scalable Vector Graphics), XSLT can output any text format, including HTML or CSV (comma-separated values). Also part of the XSL specification, the XSL/FO language displays elements on a page. Its main application: generating PDF documents.

1 | 2 | 3 | 4 |  Next >
Resources