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
Formatting is the reverse of converting and uses the same rules for arrays, collections and single values but reversed. See below.
| Display field | |||
|---|---|---|---|
| Source field | Single value | Array | Collection |
| Single value | Yes | ||
| Array | List1 | Yes | Yes |
| Collection | List1 | Yes | Yes |
1 Requires a list conversion annotation.
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.