BigDecimal 在 Velocity、Struts2 中计算为字符串

发布于 2024-08-08 01:46:15 字数 371 浏览 6 评论 0原文

我将 struts2 与 Velocity 1.5 和 Velocity Tools 1.3 一起使用。在我的模板中,我想做一个像这样的循环:

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

${item.qty} 是一个 BigDecimal,但它似乎可能作为字符串传递给 Velocity。因为这个循环不起作用。替换为 $count = 5 效果很好,打印 ${item.qty} 给出的结果是 5。Velocity 1.5 和 Tools 1.3 是 Struts2 支持的最高版本。有想法吗?解决方法?谢谢

I am using struts2 with Velocity 1.5 and Velocity Tools 1.3. In my template I want to do a loop like:

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

${item.qty} is a BigDecimal but it seems like its passed to Velocity as a String maybe. Since this loop does not work. Replacing to $count = 5 works fine, and printing ${item.qty} gives me a result of 5. Velocity 1.5 and Tools 1.3 is the highest version Struts2 will support. Ideas? Workarounds? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2024-08-15 01:46:15

我认为您需要将其转换/转换为整数才能使循环正常工作。

#set ($count = $item.getQty().intValue())

I think you need to cast/convert it to an integer for your loop to work.

#set ($count = $item.getQty().intValue())
凡间太子 2024-08-15 01:46:15

也许您需要实现自己的迭代器 - 它只会存储 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();}
}

perhaps you need to implement your own iterator - it will just store the start and end of the list of bigDecimals, and return the current one. This way, you can have an unlimited sized list of numbers (i assume that is what you wanted because you are using BigDecimals. Otherwise, just use an int or a long):

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

and

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();}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文