1

Velocity1.5とVelocityTools1.3でstruts2を使用しています。私のテンプレートでは、次のようなループを実行したいと思います。

#set ($count = ${item.qty})
#foreach($i in [1..$count])
    ${item.price}
     ...........
#end

$ {item.qty}はBigDecimalですが、おそらく文字列としてVelocityに渡されたようです。このループは機能しないため。$ count = 5に置き換えると正常に機能し、$ {item.qty}を印刷すると5の結果が得られます。Velocity1.5とTools1.3は、Struts2がサポートする最高のバージョンです。アイデア?回避策?ありがとう

4

2 に答える 2

0

おそらく、独自のイテレータを実装する必要があります。これは、bigDecimalsのリストの開始と終了を格納し、現在のイテレータを返すだけです。このようにして、無制限のサイズの数値リストを作成できます(BigDecimalsを使用しているため、これが必要なものだと思います。それ以外の場合は、intまたはlongを使用してください)。

#set ($countIterator = ${item.qtyIterator})
#foreach($i in $countIterator)
    ${i}
     ....use $i as a string...
#end

public class QuantityIterator implement Iterator<BigDecimal> {
   QuantityIterator(BigDecimal start, BigDecimal end) { this.start = start;this.end=end;}
   //..implement the iterator methods like hasNext() etc
   public hasNext() {return this.current.compareTo(this.end) < 0;} //current <= end
   public BigDecimal next() {
      if (!hasNext()) {
         throw new NoSuchElementException();
      }
      this.current = this.current.add(BigDecimal.ONE);
      return this.current;
   }
   public void remove(){throw new UnsupportedException();}
}
于 2009-10-20T05:47:17.120 に答える
0

ループを機能させるには、整数にキャスト/変換する必要があると思います。

#set ($count = $item.getQty().intValue())
于 2009-10-20T05:37:36.750 に答える