<input type="checkbox" name="city" value="NYC"
New York City
<input type="checkbox" name="city" value="PAR"
Paris
■Tip By default, all of the Spring macros will close input field tags with the HTML 4–compliant tag closure >.
If your templates should be XHTML compliant, you can instruct the macros to use XHTML tag closures />by
setting a variable in the template. For Velocity, place the line #set($springXhtmlCompliant = "true")
in your template before calling any of the macros. For FreeMarker, use <#assign xhtmlCompliant =
true in spring>.
As an added benefit, the example highlights how you can use codes for the values that will
be submitted in the form fields, but present different text values to the user. In this example,
we used city codes as the form field values and the full names of the cities in the presentation.
This is a very common requirement in web applications.
Other Macros
There are a couple of macros in the table that we didn’t discuss. They are
- #springMessage
- #springMessageText
- #springUrl
The two message macros can be used to specify localized messages based on text
held in resource bundles. If your ApplicationContextdefines a MessageSourcebean (highly
recommended), then its resource bundles will be searched to find the key that is passed as
the parameter to the macro. The second of the two permits you to specify a default message
value that will be used if the key cannot be found in the MessageSources.
#springUrllets you use URLs in your templates that include the context root of the web
application, without your template needing to know what the context root is. Having any part
of your web application aware of the context root at which it is deployed makes it significantly
less portable. For example, the fragment #springUrl("/mycontroller.html")will be rendered
as /myapp/mycontroller.htmlif your application was deployed at a context root of myapp.
Number and Date Tools
Velocity makes available several useful tools from its supporting velocity-tools-generic.jar
(you’ll need to add these your WEB-INF/libto take advantage of them). Two of these are the
NumberTooland the DateTool. Both accept a Localeobject in order to parameterize them for
specific locales, but Spring offers a further benefit in that the Localeparameter can be
retrieved using standard Spring LocaleResolverinstances that we discussed earlier. That’s
great news, and Listing 8-30 shows how to do it in a VelocityViewdefinition configured by
CHAPTER 8 ■SUPPORTED VIEW TYPES 247