Java Reference
In-Depth Information
The <s:select> tag creates a select box that allows a single selection
by default. Stripes offers helper tags that generate the options of the
select box from a Collection , an enum , or a Map . In our case, we want
the options to be the list of folders. We can obtain this list from the
FoldersViewHelper class that we wrote on page 156 . Here's the code to
create the select box:
Download email_19/web/WEB-INF/jsp/common/message_action.jsp
<jsp:useBean class="stripesbook.view.FoldersViewHelper" id="folders"/>
Move to folder:
<s:select name="selectedFolder">
<s:option value=""> Select a folder... </s:option>
<s:options-collection collection="${folders.folders}"
value="id" label="name"/>
</s:select>
<s:submit name="moveToFolder" value="Move"/>
<s:errors field="selectedFolder"/>
By using <s:options-collection> within <s:select>, we can easily gen-
erate options from the list of folders. Each option has a value and a
label obtained by calling the value= and label= properties on each object
of the collection. Here, getId ( ) and getName ( ) are called on each Folder
object. The user sees the name of the folder as each option of the select
box, and the folder's ID is set on the selectedFolder property of the action
bean.
Also notice the first option, labeled “Select a folder....” The <s:option>
tag creates a single option that's added to the select box. But we don't
want this option to be a valid selection—we're just using it so that the
user sees the “Select a folder...” message in the select box. We can use
value="" so that the option sends the empty string as a value. Since
Stripes treats empty strings as null , the select box acts like a blank input
field when the “Select a folder...” option is selected. By making select-
edFolder a required field with @Validate(required=true, on="moveToFolder") ,
clicking Move without selecting a folder will show an error message:
This way of distinguishing the first option as a message to the user (and
not a valid option) is much more elegant than putting a value of -1 or
some other “magic number” for that option. Our code is not polluted
with checks for this special value in the action bean and the manual
creation of a required-field validation error. Using an empty string and
making the field required with @Validate fits in very naturally into the
Stripes validation mechanism.
 
Search WWH ::




Custom Search