Java Reference
In-Depth Information
outside the allowed range. A select box makes the valid choices more
obvious and reduces the chance of error:
We'll generate a select box by looping from 0 to 5 in a <c:forEach>
tag and generating an option for each value of the loop. We could use
begin=0 and end=5 in the <c:forEach> tag, but that doesn't feel right.
We'd be duplicating the minimum and maximum values specified in the
validation. If we change minvalue= or maxvalue= in @Validate , we'd have
to remember to go in the JSP and change the values in begin= and end=
as well.
There's a better way. Stripes provides information about validations at
runtime with the ValidationMetadata interface. By retrieving this infor-
mation for the numberOfAliases field, we can make the minimum and
maximum values dynamically available for the JSP to retrieve:
Download email_19/src/stripesbook/action/RegisterActionBean.java
public int getMinAliases() {
return getAliasValidation().minvalue().intValue();
}
public int getMaxAliases() {
return getAliasValidation().maxvalue().intValue();
}
private ValidationMetadata getAliasValidation() {
return StripesFilter.getConfiguration()
.getValidationMetadataProvider()
.getValidationMetadata(getClass())
.get("numberOfAliases");
}
We're now ready to create the select box with the
<s:select>
tag and
nested <s:option> tags:
Download email_19/web/WEB-INF/jsp/register.jsp
<s:select name="numberOfAliases">
<s:option value="" label="How many aliases?"/>
<c:forEach begin="${actionBean.minAliases}"
var="index" end="${actionBean.maxAliases}">
<s:option value="${index}" label="${index}"/>
</c:forEach>
</s:select>
 
 
Search WWH ::




Custom Search