displaytagで通貨をフォーマットする方法
-
06-07-2019 - |
質問
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を使用できます
解決
DisplayTabはJSTLやELにあまり適しておらず、そのスタイルの形式をサポートしていません。代わりに、TableDecoratorクラスを拡張し、display:tableタグのdecorator属性を使用してそれへの参照を配置する必要があります。
デコレータサブクラスは、フォーマットされた通貨列のゲッターメソッドを定義する必要があります。次のようなものです。
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>
別の方法として、DisplaytagColumnDecoratorインターフェースを実装し、JSPからそのデコレーターを参照できます。
<display:table name="myList">
<display:column property="myProperty" title="My Property"/>
<display:column property="currency" title="Currency" decorator="test.MyColumnDecorator"/>
</display:table>
詳細については、ドキュメントを参照してください
他のヒント
クラスには何が必要ですか? 次のように記述できます。
<displaytag:column property="amount" format="$ {0,number,0,000.00}"/>
デコレータを使用できます。
次のようなものがあります
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>
から
id :uidを参照
uid :これを識別するために使用される一意のID 表。を表すオブジェクト 現在の行もに追加されます この名前のpageContextおよび 現在の行番号は キーuid_rowNum。同じ内の2つのテーブル ページに同じuidを含めることはできません(ページング 並べ替えは両方に影響します)。ない場合 <!> quot; htmlId <!> quot;同じ値が指定されている のHTML IDに使用されます 生成されたテーブル
使用するものは次のとおりです。
<d:column title="Cost" property="cost" format="{0,number,currency}" sortable="true"/>