Expert Spring MVC and Web Flow

(Dana P.) #1
#springBind("command.firstName")
<input type="text" name="$status.expression"
value="$!status.value" /><br>
#foreach($error in $status.errorMessages)
<b>$error</b> <br>
#end
<input type="submit" value="submit"/>
</form>
</body>
</html>

■CautionVelocity imports macro definitions into a global namespace, which means all macros are avail-
able from all your templates once they have been loaded. FreeMarker, on the other hand, allows macros to
be subdivided into separate namespaces. When using the Spring macros in FreeMarker you have to import
them into each template that you wish to use them in. Add the following line to the top of your FreeMarker
templates:<#import "spring.ftl" as spring />.

The binding behavior is exactly the same as we outlined for JSP bind support. It uses the
same code under the covers. If you submit a valid form, the command object will have its cor-
responding field set to the user-submitted value. Conversely, if validation errors occur such as
the field being left blank, the form will be redisplayed, and each error in the
status.errorMessagesfield can be highlighted to the user. If a value had been submitted in the
field, the original incorrect value will be redisplayed in the input field.
So far, so simple. It’s no different from JSP’s <spring:bind>tag behavior, other than the
syntactical differences in the template language.
For Velocity and FreeMarker, however, you have (since Spring 1.1) been able to enjoy an
additional suite of macros that build upon #springBindand generate a lot of the form field
content for you based on optional parameters that you supply. Table 8-2 shows the full range
of macros available to your templates.

■Tip In FreeMarker, the two macros marked (*) in the following table exist but are not actually required, as
you can use the normal formInputmacro, specifying hiddenor passwordas the value for the fieldType
parameter.

It’s a fairly comprehensive set of form-handling functionality. We’ll look at a few common
examples and leave you to explore the remainder within your own applications. Listing 8-25
shows a fragment of the beginSearch.vmtemplate in the example application. It uses the basic
#springFormInputmacro to generate a couple of input fields.

242 CHAPTER 8 ■SUPPORTED VIEW TYPES

Free download pdf