Expert Spring MVC and Web Flow

(Dana P.) #1

Exposing the Model


As always, the specific Viewimplementation is responsible for knowing what to do with a
model generated by your Controllers. For VelocityView, each object in the model is added
to a VelocityContextinstance, which is what the Velocity engine uses to merge with the
template. FreeMarker works happily with Mapobjects as models, and so no conversion is
necessary.

The Template Language


Before moving on, let’s take a sneak peek at the actual template files used for the home page.
Listing 8-19 shows an example of Velocity Template Language (VTL). In fact, it’s the familiar
home page of the sample application converted from the JSP example in Listing 8-5.

Listing 8-19.Sample Home Page in VTL

<body>
<h1>Welcome to the Flight Booking Service</h1>

<p>We have the following specials now:</p>

<ul>
#foreach ($special in $specials)
<li>
${special.departFrom.name} -
${special.arriveAt.name} from
$${special.cost}
</li>
#end
</ul>

<p><a href="search">Search for a flight.</a></p>
</body>

Listing 8-20 shows the FreeMarker equivalent for completeness. From here on, we’ll
mostly show just Velocity examples unless there are noteworthy differences in FreeMarker
handling.

Listing 8-20.The Home Page Again—This Time in FTL

<body>
<h1>Welcome to the Flight Booking Service</h1>

<p>We have the following specials now:</p>

<ul>
<#list specials as special>
<li>
${special.departFrom.name} -
${special.arriveAt.name} from

238 CHAPTER 8 ■SUPPORTED VIEW TYPES

Free download pdf