Supported View Types
Spring provides first-class support for the majority of view technologies that you’re likely to
want to use in your Web MVC application. If you need a view that isn’t supported, then you
can implement your own Viewor take advantage of additional functionality by extending
AbstractViewor another subclass.
As we investigate the various different types of views that Spring supports out of the box,
we will concentrate on only the Spring integration of those technologies. A discussion of the
working of each of them would be way beyond the scope of this chapter. Please refer to the
documentation for any third-party libraries such as FreeMarker, Velocity, JasperReports, and
others in order to get the full picture on what that particular view type can do for you.
Let’s now dive into some specific technologies and find out what additional benefits you
can gain from Spring’s integration. We’ll cover JSP/JSTL, Velocity and FreeMarker, XSLT,
and document-based views (Excel, PDF). In addition, we’ll take a brief look at Tiles and
JasperReports.
JSP and JSTL
JavaServer Pages ( JSP) is the only view technology mandated by Sun’s J2EE specification and
therefore the only technology that server vendors are compelled to support. This naturally
makes them the most popular way to generate web pages in J2EE applications.
JSP has not been without its share of criticism in the community, and the arguments
against it are well documented. Essentially, the major issue is the ease with which domain
logic and even data access logic can be coded into JSPs since they are compiled into servlets
and have the full power of the Java language available to them. We firmly believe that you
should treat JSP strictly as a view layer technology in your applications, the goal of which is to
display the model generated by your Controller. That means that you should not be able to
reference a JSP outside of the application—in other words, you should keep them inside the
WEB-INFdirectory of the WAR file where they are immune from client access.
Spring deals with JSP views via the InternalResourceViewclass (the name gives you
a strong hint about how they should be used!), which subclasses AbstractUrlBasedView.
The most popular choice of ViewResolvers for JSP are InternalResourceViewResolverand
ResourceBundleViewResolverThe example in Listings 8-1 and 8-2 demonstrates best how
you can address a JSP to display your carefully crafted model.
223
CHAPTER 8
■ ■ ■