Numbers not formatted properly in Struts 2

≡放荡痞女 提交于 2019-12-01 11:07:39

问题


I am using following syntax to display a value in a proper number format, e.g. 1,250.00.

<s:property value="%{getText('{0,number,#,##0.00}',#plan.amount)}" />

However, it is not working. The plan is an object with a property amount.


回答1:


If the value is printed like 1250.00 then it's not formatted properly. The method getText() has many overloaded methods and which method is used is determined by the parameter's types and count.

To pass arguments to the getText() method you can use OGNL list construction {}. And the arguments should be listed as single values, not "list of object". Perfectly it should be list of Double with one element inside the list.

<s:set var="amount" value="%{1250.0}"/>
<s:property value="%{getText('{0,number,#,##0.00}',{#amount})}" />



回答2:


First of all, if you want to display multiple values from a list, you need an iterator;

second, if plan is a list in the action with a getter method

public List<Something> getPlan() { return plan; }

then you don't have to put the # ahead of the variable.

The right code for your case would be:

<s:iterator value="plan">
    <s:property value="getText('{0,number,#,##0.00}',{amount})" />
</s:iterator>

There's a related Q&A on the topic.


EDIT

Since you have

<s:iterator value="list" var="plan" status="status"> 
    <div class="values"> 
        $ <s:property value="%{getText('{0,number,#,##0.00}',#plan.amount)}"/> 
    </div>
</s:iterator>

Then it should be:

<s:iterator value="list">
    <s:property value="getText('{0,number,#,##0.00}',{amount})" />
</s:iterator>


来源:https://stackoverflow.com/questions/35853834/numbers-not-formatted-properly-in-struts-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!