struts2 format string to number without commas and trim trailing zeros

I am using Struts2 to format a string to number with trim trailing zeros.

messages.properties :

format.number={0,number,#}

jsp code :

<s:text name="format.number">
    <s:param name="value" value="%{key.policyNumber}" />
</s:text>

key.policyNumber is a String, I am getting the error as "Error 500: java.lang.IllegalArgumentException: Cannot format given Object as a Number"

UPDATE: I tried the same code giving a Integer then it works. like

<s:text name="format.number">
    <s:param name="value" value="%{key.orderNum}" />
</s:text>

action class

Map<PolicyObj, CustInfo> mapObj = getDBPolicyDetails();

In DB class

PolicyObj.setPolicyNumber("0000111111");

Answers


Small change in JSP code. I kind of tricked by converting the string to number.

Here is the code.

<s:text name="format.number">
    <s:param name="value" value="key.policyNumber * 1" />
</s:text>

multiplying by 1 is one change I did and it worked.


Struts2 uses the MessageFormat API to do the formatting and if you look at the java.text.NumberFormat class, there is no format method which takes a String as a parameter.

I don't think you can use formatting in this way. You'll have to convert the string first before you can format it with Struts2 in this manner.


Need Your Help