Expert Spring MVC and Web Flow

(Dana P.) #1
■NoteThe Eclipse IDE (and consequently IBM’s WSAD) can be extended with support for both Velocity
and FreeMarker template editing through the use of two open-source plugins. See http://veloedit.
sourceforge.netand http://freemarker.sourceforge.net/eclipse.html,respectively. Your
mileage may vary with other IDEs.

Basic Configuring for Template Engines


The Velocity (or FreeMarker) engine needs to be configured using one of the Spring-supplied
classes. This usually happens in the servlet context file. Listing 8-17 shows you how.

Listing 8-17.Configuring the Velocity Engine

<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"
<property name="resourceLoaderPath" value="WEB-INF/velocity"/>
</bean>

■NoteFor FreeMarkerConfigurerthe property name for the template loading path is
templateLoaderPath,although from Spring version 1.3 that may be deprecated and you should use
resourceLoaderPathfor either.

VelocityConfigurer(and FreeMarkerConfigurer) wrap access to the actual template
engine (VelocityEngineand Configuration,respectively). The most important property to set
on these objects is the one highlighted in the example. The resourceLoaderPathdetermines
where Velocity will look for your template files. As with JSP, it’s a good idea to make these inac-
cessible to web clients by placing them somewhere inside the WEB-INFdirectory.
There are many important properties that can be set on the respective configurer beans,
some of which we’ll explore later. For now, let’s complete the integration by setting up a
ViewResolver.
Spring’s general ViewResolvers (ResourceBundleViewResolverand XmlViewResolver) are
perfectly adequate for any kind of View, template views included. But often, when your appli-
cation consists of only one view type it makes sense to take advantage of a specific resolver to
keep things a little simpler. For Velocity and FreeMarker, Spring offers VelocityViewResolver
and FreeMarkerViewResolver. Figure 7-5 in the previous chapter showed where these resolvers
fit into the general hierarchy. Listing 8-18 shows a simple configuration of them.

Listing 8-18.ViewResolver Configuration

<bean id="velocityResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="prefix" value="" />
<property name="suffix" value=".vm"/>
</bean>

236 CHAPTER 8 ■SUPPORTED VIEW TYPES

Free download pdf