Formatting Reference

Version 4 by Tim Fennell
on Jul 21, 2006 04:41.

compared with
Current by Samuel Santos
on Sep 10, 2011 20:47.

This line was removed.
This word was removed. This word was added.
This line was added.

Changes (4)

View Page History

Stripes' philosophy when it comes to formatting is pretty simple:
* Formatting should be easy (duh)
# Determine the type of object being output (String, Date, Number, Other?)
# Ask the [FormatterFactory|] for a [Formatter|] instance that can format the relevant type
# If there is a {{Formatter}} invoke it to format the object, otherwise usee {{String.valueOf()}}

Other than the object itself, formatting in Stripes is driven by two parameters (supplied as attributes on the Stripes input tags). The parameters are {{formatType}} and {{formatPattern}}. {{formatType}} is used to specify the type of information which should be output from the source object (e.g. for a {{java.util.Date}} object, should it be date, time or both). {{formatPattern}} can be either one of a set of named styles, or an explicit pattern as understood by the underlying {{java.text.Format}} object. This will all make a bit more sense in the context of the two concrete Formatters supplied with Stripes.
|short|Output information in as compact a manner as possible form. Maps to {{DateFormat.SHORT}}|
|medium|Output information in a concise, slightly less compact form. Maps to {{DateFormat.MEDIUM}}||
|long|Output information in a fairly verbose form. Maps to {{DateFormat.LONG}}||
|full|Output information in an extremely verbose form. Maps to {{DateFormat.FULL}}||
Stripes supplies a [DefaultFormatterFactory|] which knows how to supply the built in Date and Number formatters based on the inputs provided. In order to integrate a new formatter, or to substitute a different formatter for Date or Number types, you will have to provide your own implementation of {{FormatterFactory}}. This is easily done by subclassing {{DefaultFormatterFactory}} and overriding {{getFormatter()}} to perform any specific lookups and then delegating to {{super.getFormatter()}}. You are, of course, free to directly implement {{FormatterFactory}}, just bare in mind that if you do this you will need to handle the lookups for Date and Number types too.

Now all that's left is implementing your own {{Formatter}} class. This is a relatively simple operation. Remember that as your inputs you will receive an object to be formatted, a {{Locale}}, a {{formatType}} and a {{formatPattern}}. You may choose to use, or ignore as many of these parameters as you see fit. The following is an example of a custom Formatter for formatting social security numbers: