You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ted Husted <hu...@apache.org> on 2001/06/01 15:51:44 UTC

Re: Proposed feature: Bean property transformations

What I'm missing is a comprehensive, general package for converting data
types and formatting properties for presentation. Most of this
functionality is available somewhere in java and javax space, but it's
spread around.

What would be most useful, I think, is a single, generic package that
provided

(1) validation of Strings using regular expressions (a la David
Winterfeldt's servlet), with direct support for native and JDBC
datatypes,

(2) binary to String and String to binary conversions for all native and
standard types, and support for adding others,

(3) given a formatting specification ("00#.##") and data of any
supported type, return a formatted presentation String, 

(4) support for locale-senstive transformations with (3),

(5) support for extending the formatting specification for unusual
circumstances, and

(6) provide simple date-calculation methods and a countdown presentation
format (seconds, minutes, hours, or days from now until then).

We could then use this helper object during the validation cycle to
convert incoming Strings to the other types needed by business-logic
objects, AND pass through the functionality from a <bean:writeTransform
> tag, that could pull a property from a given bean, transform it, and return a formatted String for direct use by the view. 

If there is not something like this already out there, I've very
interested in getting started on this package, since I really, really
need it for my own projects. Could be a nice addition to the Commons ... 

I'm cross-posting this to Struts user in case someone can suggest a
package that already provides this functionality.

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/


Ron Smith wrote:
> 
> 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