Expert Spring MVC and Web Flow

(Dana P.) #1

  • When she resubmits the form, the check box field is not transmitted at all, and so
    Spring doesn’t bind a value to it.


•Now, her original selection (check box selected) remains on the command object!

There is fortunately a nice solution to the problem. You can place a hidden field on the form
with the same name as the check box field but prefixed with an underscore (_). Listing 8-9 shows
a snippet of just such a form.


Listing 8-9.Hidden Check Box Identifier in the Form


<form ...>



name="${status.expression}"
value="${status.value}"
checked="checked"
> I have read the terms and conditions
<input type="hidden" name="_${status.expression}"/>
</spring:bind>



When your Controllerreceives the form input the second time, it will see a POST param-
eter with the name _command.acceptTermsAndConditionsand know to look for a corresponding
parameter of the same name, minus the leading underscore. If it doesn’t find it (as it won’t in
our example), then it knows to reset the value for the field on the command object. Resetting
the value depends on the type of field the check box is bound to. If boolean, the field is set to
false. If it’s a String[], the field will be set to an empty array, and if it’s a Collection, it will be
set to null.


HTML Escaping


Tags might sometimes emit values that are unsafe for HTML and need to be escaped. Charac-
ters such as ampersands (&) and apostrophes (') have special meaning and can be replaced
with HTML entities such as &and '. There are three ways that you can force conver-
sion of these characters to their entity name counterparts in a Spring MVC application.
The coarsest level of granularity for specifying HTML escaping is application-wide. In
your web.xmlfile, you can force a default behavior which will be honored by the Spring tag
libraries (and Velocity/FreeMarker macros, which we’ll see later). Listing 8-10 shows a snippet
of the configuration.


CHAPTER 8 ■SUPPORTED VIEW TYPES 231
Free download pdf