I've been thinking of implementing this feature lately and I haven't
seen it proposed on this list yet.  Any comments?

Summary:
Provide a means to flexibly plug in transformations that could
be applied to JavaBean properties for presentation in a JSP page.
What transformation to apply to which JavaBean property
is specified in the Struts JSP tags (e.g. bean:write).
Transformations are Java classes that are responsible for taking
a JavaBean property or any other Object, applying whatever
transformation, and returning the transformed Object for
presentation in a JSP page.
Some example transformations are to format a date in a specific
format, format decimal numbers, or even to sort a collection in
a particular order before iterating over the collection's objects.

Motivation:
Separating business entity code from presentation-specific code
is a good thing.  Consider a business entity class called Order.  If
we want to display the orderPlacedDate attribute in 4 different
date formats on a JSP page, we could add 4 different methods to
the Order class to support these 4 different formats.  But we quickly
end up with a very cluttered Order class and the Order class is too
coupled to the presentation details.
One approach I've used is to create presentation wrapper classes
which hold references to the business entity objects and are
responsible for all of the presentation specific formatting.  The
JSP pages access the presentation wrapper classes and not the business
entity classes.  For sites that access many different business entity
classes, this can become very tedious.
A better approach would be to be able to plug-in specific types of
presentation transformations to be applied to specific JavaBean
properties that are to be displayed in a JSP page without having to
create unecessary wrapper classes.

Details:
Transformations are coded in transformation classes, all of which
implement
a Transformation interface.  This interface has one public method:
Object transform(Object inObj)
This method is responsible for applying whatever transformation is
needed
to the passed in object and returning a transformed version of the
object for presentation in a JSP page.
The transformation objects would be created at initialization based on
the
configuration file, and could be initialized with some parameters from
the
configuration file (e.g. the date format string to be used for a date
transformation).  Each transformation has a name associated with it, and
is
registered in a hash table based on the name.

Some of the Struts custom JSP tags would be modified to take an
additional
transformation parameter which indicates what transformation is to be
applied.  For example:
   <bean:write name="order" property="orderPlacedDate"
     transformation="shortDateFormat"/>
In the above example, the orderPlacedDate property is retrieved
from the order bean, then the Transformation named "shortDateFormat"
is looked up in the transformations registry, and applied to the
property.  Whatever was returned by the transformation is what gets
displayed on the web page.

Another benefit is that because the transformation objects exist
throughout the life of the application, some objects such as DateFormat
and NumberFormat can be cached in the transformation objects for
efficiency purposes.

There's more details, but that's the basic idea.

Ron

Reply via email to