Posted By

springett on 01/11/07


Tagged

java struts JSP


Versions (?)

Struts bean:write format attribute


 / Published in: Java
 

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

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

  1. <bean:write name="myForm" property="amount" format="$#,000.00" />
  2.  
  3. <bean:write name="myForm" property="amount" formatKey="myapp.currency.format" />

Report this snippet  

You need to login to post a comment.