2009-06-22 49 views
2

我得到的数额类似于4567.00,8976.00等。现在,在显示标签中显示此值时,我想将其打印为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的格式属性将MessageFormat:列

回答

3

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> 

请参阅the documentation获取更多信息

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 tag reference

ID:看UID

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

+0

** $ **正如我所说的这不是我想要做的。但thanx的装饰概念 – 2009-06-23 04:41:41

8

你需要什么你的类? 如下你可以写:

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

如何格式化货币,如果不是数字,则属性的类型是String。我尝试了`$ {0,string,0,000.00}`和$`{0,String,0,000.00}`,但它不起作用。 – 2012-04-30 16:03:30

0

下面是我用:

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