Java Reference
In-Depth Information
else if ("parens".equalsIgnoreCase(formatType)) {
format = "(%s) %s-%s";
}
else {
throw new StripesRuntimeException(String.format(
"Invalid phone number formatType: %s. Valid values "
+ "are 'dashes' and 'parens'.", formatType));
}
return String.format(format, phoneNumber.getAreaCode(),
phoneNumber.getPrefix(), phoneNumber.getSuffix());
}
}
PhoneNumber objects can now be formatted with <s:format>, using for-
matType= "dashes" or formatType= "parens". Since the formatter is in an
extension package, it will be loaded by Stripes. We can display phone
numbers in the contact view and in the text field of the contact form in
a consistent format, no matter how the phone number was entered by
the user. For example:
Download email_07/web/WEB-INF/jsp/contact_view.jsp
<td class="label"> Phone number: </td>
<td class="value">
<s:format formatType="dashes"
value="${actionBean.contact.phoneNumber}"/>
</td>
Download email_07/web/WEB-INF/jsp/contact_form.jsp
<td> Phone number: </td>
<td>
<s:text name="contact.phoneNumber" formatType="dashes"/>
</td>
The formatType= attribute could also have been omitted to use the for-
matter's default.
Using a Different Date Pattern Just for One Field
Back on page 102 , we discussed changing the patterns for the date
type converter, which affects all date input fields in the application.
But what if we want to use a different pattern just for one field? For
example, we might have a field where we expect the user to enter a time
with no date, such as "10:30" . We want to use the "HH:mm" pattern for
that field without changing the patterns used for other date fields.
In a case like this, we're better off creating a separate type converter
that parses the time only and not configuring it as an automatically
loaded extension so that it doesn't replace the default type converter.
 
 
Search WWH ::




Custom Search