2

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

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

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

创建一个新类:

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
    }
}

现在在显示标签中

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

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

4

4 回答 4

9

你需要你的课做什么?你可以这样写:

<displaytag:column property="amount" format="$ {0,number,0,000.00}"/>
于 2009-07-23T15:15:15.800 回答
4

DisplayTab 对 JSTL 或 EL 不太友好,并且不支持那种格式的格式。相反,您需要扩展 TableDecorator 类并使用 display:table 标记的 decorator 属性对其进行引用。

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

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

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

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

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

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

于 2009-06-22T16:04:46.707 回答
1

你可以使用装饰器。

你会有类似的东西

class myDecorator extends TableDecorator{

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


   return "$"+myClass.getCurrency;

 }
}

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

如果不想使用装饰器,可以使用 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>

来自display:tag 标签参考

id : 见 uid

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

于 2009-06-22T16:08:29.207 回答
0

这是我使用的:

<d:column title="Cost" property="cost" format="{0,number,currency}" sortable="true"/>
于 2012-06-21T20:00:56.960 回答