Return to Snippet

Revision: 2180
at January 11, 2007 05:30 by springett


Initial Code
<bean:write name="myForm" property="amount" format="$#,000.00" />

<bean:write name="myForm" property="amount" formatKey="myapp.currency.format" />

Initial URL
http://chintanrajyaguru.com/index.php?option=com_content&task=view&id=50&Itemid=9

Initial Description
You can use these attributes to format the display of your form bean properties. This works pretty well in displaying currency and date/time values. For example, you can display 1234.567 as $1,234.57 and 08/12/2006 as Aug 12, 2006. 

In order to get the second tag to work you will need to add the following line to your resource bundle:

myapp.currency.format=$#,000.00

The preferred way is to use the second format because it makes the code portable. Plus, if you want to change the format, you can make the change in resource bundle without having to worry about changing all your JSPs.

The type of the property being formatted cannot be String. The format attribute simply uses default format (display the property as is) when the property is of type java.lang.String. In other words:

private double amount; // will be formatted correctly
private String amount; // will NOT be formatted at all

Initial Title
Struts bean:write format attribute

Initial Tags
java

Initial Language
Java