Thanks for that, Oleg, now here's my take on this.
I have created another tag called WriteNumberTag which contains lots of code
stolen from WriteTag and MessageTag, and contains a mix of parameters from
the pair of them.
Usage is like WriteTag, except that you *must* supply a property parameter.
e.g.
<bean:writenumber name="mybankaccount" property="value"/>
What will then happen is as well as calling getValue() on the mybankaccount
bean, it will also call getValuenumberformat(). This returns a string which
is a key into your application resources. The value of this key represents
the format string which is passed into a DecimalFormat object. e.g.
mynumberformat.test1=#,##0.00;(#)
The value returned from getValue() is then formatted with the resultant
string. Note the getValue function could either return a primative (such as
int) or a wrapper object (such as java.lang.Integer).
If the getValue function returns a wrapper object and that object is null,
WriteNumberTag will take the format key you returned from
getValuenumberformat() and append “_null” to the end and tries to look this
up in the application resources, writing this value out if it exists. e.g,
mynumberformat.test1_null=somenulldisplayvalue
If you don’t like the idea of providing a getXxxxxnumberformat() function,
then a property in the tag called formatkey bypasses this and just uses this
key. e.g.
<bean:writenumber name="mybankaccount" property="value"
formatkey="mynumberformat.test1" />
That’s right, it’s a KEY, not a format. If I made it just a format string
then it would just encourage you guys to write non internationalised stuff
:-)
Obviously it tries to use the current session locale, but you can override
this in the tag.
Properties supported through tag:
------------------------
filter, name, property, scope – just like bean:write
locale, bundle – just like bean:message
formatkey – new, see above.
implementation notes:
------------------------
The DecimalFormat objects are cached for speed (these things are probably
going to be used a lot). The cache is a map keyed on
formatkey_localeLanguage_localeCountry
Yes, I should really cache the DecimalFormatSymbols objects.
And yeah, the formatting is pants. I use VisualAge for Java which means I’m
looking at one method at a time, not the whole file.
Anyways let me know what you think. Does anybody want to put this in the
struts distribution?
Regards,
Ned.
_____________________________________________________________________________________
Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com
getter.access=IllegalAccessException accessing property {0} of bean {1}
getter.argument=IllegalArgumentException: {0}
getter.bean=No bean found for attribute key {0}
getter.invocation=InvocationTargetException accessing property {0} of bean
{1}: {2}
getter.io=IOException: {0}
getter.method=No property getter method for property {0} of bean {1}
getter.parameter=No parameter {0} was included in this request
getter.resource=No resource {0} available in this application
getter.scope=Invalid bean scope {0} specified
getter.name=You must specify a name attribute if the property attribute is
present
messageTag.message=Missing message for key {0}
messageTag.resources=Missing resources attribute {0}
writeNumberTag.noformatter=Unable to retrieve DecimalFormat object for
message key {0}
WriteNumberTag.java
writenumber
org.apache.struts.taglib.bean2.WriteNumberTag
empty
filter
false
false
name
true
true
property
true
true
scope
false
true
formatkey
false
true
locale
false
true
bundle
false
true