Struts Tip #13 - Use Display* helpers to convert or transform properties

Ted Husted



There are many properties in an application that need to rendered in a particular way. The database may store a telephone number as a simple String of numerals, but your business requirements might say that telephone numbers must formatted with spaces or hyphens to make them easier to read.

A good way to handle these requirements is with a helper method. The helper method starts with the original database value and then modifies it to meet the business requirements. Sometimes, the value may modified in more than one way. One page may require that a person's name in all capital letters, or displayed last name first. Using the helper pattern, we can store the original value once in our transfer object and use helpers to modify it as needed.

Dates are another place where we can use helpers. Internally, the database may store a date as a binary Timestamp. But when we send the date between the business and application tiers, we would prefer to represent it as a String that the users can view and edit.

The Artimus example application uses a contributedDisplay helper property to render the Timestamp as a String and then convert it back to a Timestamp later. The ActionForm just uses the String property, contributedDisplay . the business tier, there are two properties. A Timestamp contributed property, to hold the database value, and the String contributedDisplay helper property. Here's some code from the *business logic* bean:

public static Timestamp getTimestamp() { return new Timestamp(System.currentTimeMillis()); }

private Timestamp received= getTimestamp(); public Timestamp getReceived() { return (this.received); }

public void setReceived(Timestamp received) { this.received=received; }

public void setReceivedDisplay(String receivedDisplay) { if (null==receivedDisplay) this.received = null; else this.received = Timestamp.valueOf(receivedDisplay); }

public String getReceivedDisplay() { Timestamp received = getReceived(); if (null==received) return null; else return received.toString(); }

Note that in our business logic bean (above), the receivedDisplay property has no field of its own. It gets what it needs from the contributed property, munges the value, and returns the desired result. the ActionForm, we have a typical String property:

private String receivedDisplay = getTimestamp().toString();

public String getReceivedDisplay() { return this.receivedDisplay;

public void setReceivedDisplay(String receivedDisplay) { this.receivedDisplay = receivedDisplay; }

The business bean handles the conversion issues. The ActionForm just buffers the String property.

Of course, the date-handling here is rudimentary but can easily improved by changing how the business tier method is implemented. The ActionForm and JSP code would unaffected any change in how the business tier chooses to render the date.

The Display* helper technique can used to fill whatever data conversions or transformations an application may require. The essence of this tip is to recognize that data conversion and formatting is a business requirement and should handled the business tier whenever possible.


HTH, Ted.


Struts Tips  are based on excerpts from the book Java Web Development with Struts. The tips released twice weekly on the MVC-Programmers List. To subscribe, visit BaseBean Engineering.

About Ted. Ted Husted is an active Struts Committer and co-author of Java Web Development with Struts and Professional JSP Site Design. Ted also moderates the Struts mailing list and the JGuru Struts FAQ.

Copyright Ted Husted 2002. All rights reserved.