list 如何使用 foreach 循环在 Velocity 中获取列表元素以显示为表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12685129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get list elements in Velocity using foreach loop to display as a table
提问by mdp
I have a list which i am getting from database.I have 6 elements in the list
我有一个从数据库中获取的列表。列表中有 6 个元素
List<String> list=new ArrayList<String>();
list.add("No Connection");
list.add("sep 24 ,2009");
list.add("no issues are there");
list.add("dec 30,2012");
list.add("no meter");
list.add("april 12,2013");
map.put("list",list);
I am using Velocity template. In that i am using foreach loop to get the list items.I have to display in the table with two cells in the first cell three elements in the even indexes should display and in second cell odd indexes should display.How can do that.
我正在使用速度模板。因为我正在使用 foreach 循环来获取列表项。我必须在表中显示第一个单元格中的两个单元格,偶数索引中的三个元素应该显示,第二个单元格中的三个元素应该显示奇数索引。如何做到这一点。
#foreach($mylist in $list)
<td>even indexed list elements in my list</td>
<td align="center">odd indexed elements in the list.</td>
#end
Update:
更新:
I tried like this but it doesn't work.Is this correct way of getting indexes in velocity.
我这样试过,但它不起作用。这是获取速度索引的正确方法吗?
#set ($counter = 0)
#foreach ($i in $descList)
#set ($counter = $counter + 1)
#if ( $couter % 2 == 0)
<td>$descList[$i]</td>
<td align="center"></td>
#else
<td></td>
<td align="center">$descList[$i+1]</td>
#end
#end
I am getting following error.
我收到以下错误。
07:53:31,952 ERROR VelocityEngine:43 - Left side ($couter) of modulus operation has null value. Operation not possible. /emailtemplates/diis_nem_issues_email.vm [line 47, column 20]
07:53:31,952 错误 VelocityEngine:43 - 模数运算的左侧 ($couter) 具有空值。无法操作。/emailtemplates/diis_nem_issues_email.vm [第 47 行,第 20 列]
07:53:31,953 ERROR VelocityEngine:43 - Left side ( 2 ) of '==' operation has null value. If a reference, it may not be in the context. Operation not possible. /emailtemplates/diis_nem_issues_email.vm [line 47, column 25]
07:53:31,953 错误 VelocityEngine:43 - “==”操作的左侧 (2) 为空值。如果是引用,则可能不在上下文中。无法操作。/emailtemplates/diis_nem_issues_email.vm [第 47 行,第 25 列]
回答by mdp
I resolved this issue by using hashtable in my application instead of List in my application
我通过在我的应用程序中使用哈希表而不是我的应用程序中的 List 解决了这个问题
#foreach( $key in $hashtab.keySet() )
<tr><td>$key</td><td align="center">$hashtab.get($key)</td></tr>
#end
and in my java class i have like this
在我的 java 课上我有这样的
HashTable<String,Date> hashtab=new HashTable<String,Date>();
hashtab.put(key,value);
Thanks guys for your Ideas.
谢谢你们的想法。
回答by Lucas
I'm using this snippet that works for me:
我正在使用这个对我有用的代码片段:
#set( $count = 1 )
<p>User details:</p>
<table>
#foreach( $user in $users)
<tr>
<td>$count</td>
<td>$user.username</td>
<td>$user.age</td>
</tr>
#set( $count = $count + 1 )
#end
</table>
where '$users' is a List that I'm setting in java, like this:
其中 '$users' 是我在 java 中设置的列表,如下所示:
List<User> users = ...
params.put("users", users);
I found these solution here: http://thinkinginsoftware.blogspot.com.ar/2010/03/velocity-templates-for-email.html
我在这里找到了这些解决方案:http: //thinkinginsoftware.blogspot.com.ar/2010/03/velocity-templates-for-email.html
回答by Sergiu Dumitriu
The problem is that you have a typo: couter
instead of counter
.
问题是你有一个错字:couter
而不是counter
.
回答by John Klakegg
As far as I remember you could do something like this:
据我记得你可以做这样的事情:
#set ($counter = 0)
#foreach ($mylist in $list)
#set ($counter = $counter + 1)
#if ( $couter % 2 == 0)
<td>even indexed result</td>
<td align="center"></td>
#else
<td></td>
<td align="center">odd index result</td>
#end
#end
回答by axel
There is an small mistake in the code, as follows:
代码中有一个小错误,如下:
if ( $**couter** % 2 == 0)
**couter** ->>>> **counter**