Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Dave D. Nelson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dave D. Nelson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're passing the string literal 'valorFinanciamento' to getText(). Remove the single quotes to pass the property itself.
 
Sergio Rossini
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OMG!
It works!
The simple quotes are there because the example from documentation at Apache Struts has too.
Thanks David!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link to page is here.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks; looks like it's already been fixed in the current version.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic