Java Reference
In-Depth Information
Stripes offers solid built-in type conversion support. Let's discuss how
to convert data types to
String
s: formatting.
5.3
Formatting
Formatting is type conversion in the opposite direction. An object of
type
T
must be converted to a
String
to be displayed to the user. There's
always the
toString
( ) method, but Stripes gives us a more powerful way.
With formatters, the value can easily be displayed in different ways and
can do so in a locale-sensitive manner as well.
mentation of the
Formatter<T>
interface and returns a
String
for a given
object of type
T
. Formatters are called upon when we use Stripes tags
that support formatted values, such as <s:format> and <s:text>. When
the tag refers to a property of type
T
, Stripes uses a
Formatter<T>
imple-
mentation to convert the property's value to a
String
, which is returned
to the tag.
Here is the
Formatter<T>
interface:
public interface
Formatter<T> {
void
setLocale(Locale locale);
void
setFormatType(String formatType);
void
setFormatPattern(String formatPattern);
void
init();
String format(T input);
}
Along with the user's locale, a formatter is given a type and a pat-
tern with the
formatType=
and
formatPattern=
attributes of format-aware
Stripes tags. The type indicates what to display, as in
"date"
,
"time"
, and
"datetime"
for the Stripes
Date
formatter. The pattern describes how to
display the value, such as
"short"
,
"medium"
,
"long"
, and
"full"
.
Let's look at the formatters provided by Stripes.
Built-in Formatters
Stripes comes with built-in formatters for
Date
s, all
Number
types, and
enumerated types. The date and number formatters support differ-
ent format types, named patterns, and arbitrary patterns using the
Java
SimpleDateFormat
and
DecimalFormat
syntax. The result is format-
ted according to the user's locale.
Search WWH ::
Custom Search