29-03-2012, 12:55 PM
View technologies
View technologies 23.docx (Size: 81.72 KB / Downloads: 178)
Introduction
One of the areas in which Spring excels is in the separation of view technologies from the rest of the MVC
framework. For example, deciding to use Velocity or XSLT in place of an existing JSP is primarily a matter of
configuration. This chapter covers the major view technologies that work with Spring and touches briefly on
how to add new ones. This chapter assumes you are already familiar with Section 13.5, “Views and resolving
them” which covers the basics of how views in general are coupled to the MVC framework.
14.2. JSP & JSTL
Spring provides a couple of out-of-the-box solutions for JSP and JSTL views. Using JSP or JSTL is done using
a normal view resolver defined in the WebApplicationContext. Furthermore, of course you need to write some
JSPs that will actually render the view.
14.2.1. View resolvers
Just as with any other view technology you're integrating with Spring, for JSPs you'll need a view resolver that
will resolve your views. The most commonly used view resolvers when developing with JSPs are the
InternalResourceViewResolver and the ResourceBundleViewResolver. Both are declared in the
WebApplicationContext:
Using Spring's form tag library
As of version 2.0, Spring provides a comprehensive set of data binding-aware tags for handling form elements
when using JSP and Spring Web MVC. Each tag provides support for the set of attributes of its corresponding
HTML tag counterpart, making the tags familiar and intuitive to use. The tag-generated HTML is HTML
4.01/XHTML 1.0 compliant.
Unlike other form/input tag libraries, Spring's form tag library is integrated with Spring Web MVC, giving the
tags access to the command object and reference data your controller deals with. As you will see in the
following examples, the form tags make JSPs easier to develop, read and maintain.
Let's go through the form tags and look at an example of how each tag is used. We have included generated
HTML snippets where certain tags require further commentary.