aspose file tools*
The moose likes Struts and the fly likes Struts 2: How do you display currency in a textfield? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2: How do you display currency in a textfield?" Watch "Struts 2: How do you display currency in a textfield?" New topic
Author

Struts 2: How do you display currency in a textfield?

Dave D. Nelson
Greenhorn

Joined: Jan 28, 2008
Posts: 2
I have the following Struts 2 textfield:



I would like to format the value as currency, though--specifically, US Dollars. I searched the FAQ and Struts forum for this, and found several threads with similar questions, but didn't find any particularly elegant solutions.

In this thread, Elaine Micheals showed how to format currency using the JSTL fmt:formatNumber tag:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
[...]
<fmt:formatNumber value="${numberData}" pattern="$0.00" />


However, this doesn't work for showing the value in a Struts 2 textfield.

Another thread, about mixing the struts-html & format tags sounded pretty similar to what I'm trying to do. However, the general consensus was that the best way is to do the formatting in the Action class, so the Struts tag gets an already-formatted String for its value attribute.

While this method works, it's not pretty. I'd prefer to let the UI do the formatting. Is there a better way?
chad michael davis
Author
Greenhorn

Joined: Mar 01, 2006
Posts: 27
If you take advantage of Struts 2 i18n support you can get a very elegant solution as follows.

I can define the following property, that takes advantage of Java's MessageFormat support in properties files ResourceBundles.


item.price={0,number,currency}

Now, I will pull this text resource from my bundle with the struts 2 text tag, passing a parameter that references some data I've prepared in my action.

<s:text name="item.price">
<s aram value="totalCost"/>
</s:text>


The output will be a currencty formatted price.


Is this what you had in mind?


Chad Davis<br />Co-author of Struts 2 in Action
Dave D. Nelson
Greenhorn

Joined: Jan 28, 2008
Posts: 2
Chad,

Thank you for your reply. Unfortunately, I don't think I can do what I need with that method, either.

My problem is not displaying a formatted number--I now know at least 3 different ways to go about that.

What I need is to format a number and set it as the 'value' attribute of a textfield in a Struts 2 form. So far, the only way I know of to do that is to format the number in my bean, so when I say:

The getUnitPrice() method on someObject would return an already-formatted value as a String.

Right now, getUnitPrice() returns a Float. I just feel like I should be able to decide how I want to format it from within my JSP, rather than in the bean.

Thanks,
-dave
Sergio Rossini
Greenhorn

Joined: May 20, 2009
Posts: 6
We have the same problem.
How to solve?

Using



like Apache web site says, does not work. The textfield is empty.

But the code:


show de value correctly.

Someone to help?

Thanks!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're passing the string literal 'valorFinanciamento' to getText(). Remove the single quotes to pass the property itself.
Sergio Rossini
Greenhorn

Joined: May 20, 2009
Posts: 6
OMG!
It works!
The simple quotes are there because the example from documentation at Apache Struts has too.
Thanks David!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sure thing.

What page shows the quotes? If you provide a link I'll make sure it's fixed in future versions.
Sergio Rossini
Greenhorn

Joined: May 20, 2009
Posts: 6
The link to page is here.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Thanks; looks like it's already been fixed in the current version.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2: How do you display currency in a textfield?