java 如何在 displaytag 中格式化货币

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1027969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:51:45  来源:igfitidea点击:

how to format currency in displaytag

javadisplaytagnumber-formatting

提问by Rakesh Juyal

I am getting the value of amount like 4567.00 , 8976.00 etc. Now while dispalying this value in displaytag i would like to print it as $4567.00 instead of just 4567.00. How can i do that? Provided i just want to use display tag. I can acheive the same thing using core:out tag.

我得到的值是 4567.00 、 8976.00 等。现在在 displaytag 中显示这个值时,我想将它打印为 4567.00 美元而不是 4567.00。我怎样才能做到这一点?前提是我只想使用显示标签。我可以使用 core:out 标签实现同样的目的。

$<core:out value="${variableInMyList}" />


Answer Found [ How i did it ]

找到答案 [我是怎么做的]

Create a new class:

创建一个新类:

public class NumberFormatDecorator implements DisplaytagColumnDecorator{
    Logger logger = MyLogger.getInstance ( );

    public Object decorate(Object columnValue, PageContext pageContext, MediaTypeEnum media) throws DecoratorException {        
        try
        {
            Object colVal = columnValue;
            if ( columnValue != null ){
                colVal = Double.parseDouble( (String)columnValue );
            }
            return colVal;
        }catch ( Exception nfe ){}
        logger.error( "Unable to convert to Numeric Format");
        return columnValue; // even if there is some exception return the original value
    }
}

now in display tag

现在在显示标签中

<displaytag:column title="Amount" property="amount" decorator="com.rj.blah.utils.decorator.NumberFormatDecorator" format="$ {0,number,0,000.00}"/>

Note:we can use the MessageFormat in format attribute of displaytag:column

注意:我们可以在 displaytag:column 的 format 属性中使用 MessageFormat

回答by

What do you need your class for? You could write it as follows:

你需要你的课是为了什么?你可以这样写:

<displaytag:column property="amount" format="$ {0,number,0,000.00}"/>

回答by skaffman

DisplayTab is not very JSTL or EL friendly, and doesn't support that style of formatting. Instead, you need to extend the TableDecorator class and put a reference to it using the decorator attribute of the display:table tag.

DisplayTab 对 JSTL 或 EL 不是很友好,并且不支持这种格式的样式。相反,您需要扩展 TableDecorator 类并使用 display:table 标记的装饰器属性放置对它的引用。

Your decorator subclass should define a getter method for your formatted currency column, something like:

您的装饰器子类应该为格式化的货币列定义一个 getter 方法,例如:

public class MyTableDecorator extends TableDecorator {
    public String getCurrency() {
        MyRowType row = getCurrentRowObject();
        return row.getCurrency.format();
    }
}

and

<display:table name="myList" decorator="test.MyTableDecorator">
    <display:column property="myProperty" title="My Property"/>
    <display:column property="currency" title="Currency"/>
</display:table>

Alternatively, you can implement the DisplaytagColumnDecorator interface, and reference that decorator from the JSP:

或者,您可以实现 DisplaytagColumnDecorator 接口,并从 JSP 引用该装饰器:

<display:table name="myList">
    <display:column property="myProperty" title="My Property"/>
    <display:column property="currency" title="Currency" decorator="test.MyColumnDecorator"/>
</display:table>

See the documentationfor more information

有关更多信息,请参阅文档

回答by Tom

You could use a decorator.

你可以使用装饰器。

you would have something like

你会有类似的东西

class myDecorator extends TableDecorator{

 public String getCurrency(){
   MyClass myClass  = (MyClass)getCurrentRow();


   return "$"+myClass.getCurrency;

 }
}

Check them out! http://displaytag.sourceforge.net/10/tut_decorators.html

去看一下!http://displaytag.sourceforge.net/10/tut_decorators.html

If you don't want to use decorators, you could use the id attribute and JSTL

如果不想使用装饰器,可以使用 id 属性和 JSTL

<display:table htmlId="list" name="mylist" id="row">

   <display:column>
    <%-- row is your current list object. row.currency calls getCurrency()
         $ goes right out to HTML
    --%>
     $ <c:out="${row.currency}"/>
   </display:column>
</display:table>

From display:tag tag reference

显示:标签标签参考

id: see uid

uid: Unique id used to identify this table. The object representing the current row is also added to the pageContext under this name and the current row number is added using the key uid_rowNum. Two tables in the same page can't have the same uid (paging and sorting will affect both). If no "htmlId" is specified the same value will be used for the html id of the generated table

id: 见 uid

uid:用于标识此表的唯一 ID。表示当前行的对象也被添加到此名称下的 pageContext 中,并使用键 uid_rowNum 添加当前行号。同一页中的两个表不能具有相同的 uid(分页和排序会影响两者)。如果未指定“htmlId”,则生成表的 html id 将使用相同的值

回答by Andy Malakov

Here is what I use:

这是我使用的:

<d:column title="Cost" property="cost" format="{0,number,currency}" sortable="true"/>