Search code examples
javajspstruts2strutsognl

How to format currency in Struts 2?


I have the following code in Struts

<fmt:formatNumber  type="currency" currencySymbol="Rs" value="${product.priceSell + product.priceSell * rates[nr.count - 1]}" maxFractionDigits="2" minFractionDigits="2"/>

I want to convert it into Struts2. I tried below code, but its not working.

<s:text name="format.money">
    <s:param name="value" value="%{#product.priceSell+ #product.priceSell * #rates[#nr.count - 1]}" />
</s:text>

Note: rates is an Arraylist<String>.


Solution

  • You can use JSTL fmt tag in Struts2. JSP EL expression searches all scopes and valueStack in Struts2. Analogous to this for OGNL would be #attr prefix. So, to access scoped variables, and search in all scopes you should try something like

    "%{#attr.product.priceSell+ #attr.product.priceSell * #attr.rates[#attr.nr.count - 1]}"
    

    Note, the values in the expression should not be of type String. If you keep the values as strings, then it needs to parse them before using in the expression as a valid numbers and this better do in the action rather than in JSP.