Expert Spring MVC and Web Flow

(Dana P.) #1
In the next section we’ll see how Spring enables seamless integration with another well-
known presentation technology: XSLT.

XML and XSLT


XSLT is a popular view technology in many applications. It naturally and cleanly separates
data from presentation and offers no opportunity to mix domain logic with the presentation,
much as we’ve been advocating throughout this book.
However, despite tremendous advances in some XSLT engines, it can still be quite a
heavyweight view layer in terms of processing power. Unless your application already deals
with XML in some native form, then your model beans will also have to undergo an intermedi-
ate conversion to XML prior to transformation by XSLT. You could consider employing the
W3C API, JDOM, or another XML API that you are familiar with to handle this conversion
manually, but you really need to have a good reason for doing this. If you have to develop the
stylesheets and you have only in-house expertise in JSP, Velocity, or similar, you may be best
served switching your choice of view technology.
On the other hand, if you are creating Sourceobjects in a more efficient format than XML
DOM trees, or if your team already has in-house XSLT expertise (or better, a suite of existing
stylesheets that you can take advantage of without having to develop them), then these may
be mitigating reasons for considering XSLT as the view.

Defining an XSLT View


So having weighed up the pros and cons, let’s see how to actually make use of XSLT for XHTML
generation in your web applications. First off, we define a Viewand configure it. Listing 8-32
shows how using a definition in a views.propertiesfile that will be resolved by our good
friend ResourceBundleViewResolver.

Listing 8-32.Defining an XSLT View

home.class=com.apress.expertspringmvc.flight.web.view.HomePage
home.stylesheetLocation=/WEB-INF/xsl/home.xslt

Interestingly, the Viewclass is obviously not provided by Spring but belongs to the applica-
tion itself. Why? Well, as we mentioned in the introduction to this section, the native model (your
Mapof objects) probably has to be converted to an XML representation prior to transformation
by your stylesheet. Although several libraries such as JAXB (http://java.sun.com/webservices/
jaxb), Domify (http://domify.sourceforge.net), and Castor (http://www.castor.org) could
potentially help automate this task, they may not offer the flexibility you need in generating your
representation of the object graph. Your XML representation may even exist already in the model
if you generated it in the Controlleror obtained it from the service layer of your application.

■Tip If you find that something like Domify does provide what you need, then it would be a good idea to
create a concrete DomifyViewextending AbstractXsltViewand use this for all your application needs.

250 CHAPTER 8 ■SUPPORTED VIEW TYPES

Free download pdf