2009-06-22 61 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"/>