Java ApacheVelocity-基于Foreach总计数解析VM文件

Java ApacheVelocity-基于Foreach总计数解析VM文件,java,apache,velocity,Java,Apache,Velocity,我正在尝试创建一个ApacheVelocity脚本,该脚本分别基于foreach计数1或2来执行VM文件 以下是我正在使用的代码: #set ($i = 0) #foreach ($report in $reportInfo.reportList) #set ($i = $i + 1) #if ($i == 2) #parse ("/MyReport/Report1.vm") #end #end #set ($j = 0) #foreach ($Report in $reportInfo.re

我正在尝试创建一个ApacheVelocity脚本,该脚本分别基于foreach计数1或2来执行VM文件

以下是我正在使用的代码:

#set ($i = 0)
#foreach ($report in $reportInfo.reportList)
#set ($i = $i + 1)
#if ($i == 2)
#parse ("/MyReport/Report1.vm")
#end
#end

#set ($j = 0)
#foreach ($Report in $reportInfo.reportlist)
#set ($j = $j + 1)
#if ($j == 1 )
#parse ("/MyReport/Report2.vm")
#end
#end

最终的结果是,如果foreach总数为2,它也将运行Report2.vm,因为计数为“12”。我是否可以编写代码来查看变量计数的总和、最大值或总数?

您好,我知道您想计算“for each”循环执行了多少次,apache velocity模板引擎提供了一种获取循环计数器的简单方法,以便您可以执行以下操作:

<table>
#foreach( $customer in $customerList )
<tr><td>$foreach.count</td><td>$customer.Name</td></tr>
#end
</table>

Velocity also has other convenience counters like:
1. $foreach.index instead of $foreach.count
2. $foreach.hasNext
3. $foreach.first and $foreach.last are also provided to compliment $foreach.hasNext

#foreach($customerList中的客户)
$foreach.count$customer.Name
#结束
Velocity还有其他便利计数器,如:
1. $foreach.index而不是$foreach.count
2. $foreach.hasNext
3. $还提供了foreach.first和$foreach.last来补充$foreach.hasNext

有关更多示例,请参考Apache Velocity。

听起来您只是想根据列表的大小执行一些逻辑

#if ($reportInfo.reportList.size() == 1)
#parse ("/MyReport/Report2.vm")
#elseif ($reportInfo.reportList.size() == 2)
#parse ("/MyReport/Report1.vm")
#end

你的解决方案对我来说毫无问题。尺寸是对我有用的财产。非常感谢。没问题,祝项目的其余部分好运!如果我的回答有帮助,请随意投票/接受。