Display Formatting
Introduction
Display formatting is a simple way to create a formatted version of a database record using converters. Consider an example Data Transfer Object.
public class CountryDTO implements Serializable {
private static final long serialVersionUID = 8324323834746348137L;
private int id;
private String name;
private String shortcode;
private Integer currencyId;
private Integer msisdnLength;
private Region region;
private boolean popular;
private Date lastUpdateTime;
private CurrencyVO currency;
...
}
A view helper can present a formatted version using a call to StrutsMiscellaneousLibrary.updateDisplay, which uses the conversion annotation to format from the source field of the same name. If no conversion annotation is present, the default converter for the source field type is used.
public class CountryDisplay {
private String id;
private String name;
private String shortcode;
private String currencyName;
private String msisdnLength;
@CustomConversion(validatorClass = RegionFormatter.class)
private Region region;
@DisableFormatting
private boolean popular;
@DateConversion(format = "dd/MM/yy")
private String lastUpdateTime;
public LoggerOverrideDisplay(CountryDTO country) {
StrutsMiscellaneousLibrary.updateDisplay(CountryDTO.class, country, this);
if (country != null) {
popular = country.getPopular()?"Yes":"";
if (country.getCurrency() != null) {
currencyName = country.getCurrency().getName();
} else {
currencyName = "";
}
} else {
popular = "";
}
}
...
}
Formatting Rules
Despite using converters, which only convert a single value into a single value when populating a form, display formatting can handle arrays and collections from arrays and collections. See below. Some modes use collection converters instead.
| Display field | |||
|---|---|---|---|
| Source field | Single value | Array | Collection |
| Single value | Yes | ||
| Array | Yes | Yes | |
| Collection | Yes1 | Yes | Yes |
1 Uses collection converter instead.
Notes
The StrutsMiscellaneousLibrary.updateDisplay function does not follow associations.
The formatted text created by the default enumerated converter is the value name (usually block capitals), whilst the boolean converter returns '1'. Use custom or bespoke converters for prettier text.