Expert Spring MVC and Web Flow

(Dana P.) #1

Spring JSP Tags


For the first time, we have used Spring-specific tags in our XHTML, namely the
<spring:nestedPath>and <spring:bind>tags. These tags work together to create full paths to
properties on the command bean, in order to pull values from the bean and return any valida-
tion errors on the field. These tags aren’t required, but are recommended as they provide a
standard way to retrieve metadata about a form field and its relationship to a property of the
command bean.
Listing 4-19 includes a snippet of the rendered XHTML that is sent to the browser to illus-
trate what the Spring tags are actually doing. Notice how the names of the form input elements
match the names of the properties from the SearchFlightsclass, as well as the paths from the
<spring:bind>tags.

Listing 4-19.Rendered XHTML for Search Flights Page

<tr>
<td>Depart From:</td>
<td>

<input type="text" name="departFrom" value="" />

</td>
<td>Depart On:</td>
<td>

<input type="text" name="departOn" value="" />

<span style="font-size:smaller">(yyyy-MM-dd HH)</span>
</td>
</tr>

If you recall that we declared the SearchFlightsclass as the command bean, then the
paths from the <spring:bind>tags will look familiar. The path names used in the tags are the
same as the bean’s property names.
The <spring:nestedPath>tag sets a path name that all enclosed <spring:bind>tags will
use as a prefix. We are using this tag to effectively set the name of the command bean once, to
avoid repeating it with each <spring:bind>tag.
The <spring:bind>tag will bring a statusvariable into scope, which is an object that con-
tains the metadata for a bean property from the command bean. The status.valuevariable
is the current value of the property defined by the path, retrieved from executing the getter
method. On initial page views, this won’t render anything, as the command bean has not been
populated yet. The status.expressionvariable is the name of the property itself (minus the
name of the command bean).
While it might look like overkill at this point to use the Spring tags, their true benefit
appears when validation is enabled and errors are generated. By using the <spring:bind>tag,
you can easily retrieve any errors associated with a property or retrieve the current value of the
property. When validation fails, and the page is re-rendered, these abilities make it easy to dis-
play what the user already entered into the form with the appropriate error messages.

72 CHAPTER 4 ■JUMP INTO SPRING MVC

Free download pdf