2013-03-21 72 views
3

我正在从数据库中提取对象列表。我想使用velocity模板将它们填充到html表格中。在html模板中显示速度模板中的java对象列表

<table> 
<thead> 
<tr> 
<td>$value1 </td> 
<td>$value2 </td> 
</tr> 
</thead> 
<tbody> 
<!-- Iterate through the list (List<SomeObject>) and display them here, --> 
</tbody> 
</table> 

对于我使用下面的代码头,

VelocityContext context = new VelocityContext(); 
context.put("value1", "text1"); 
context.put("value2", "text2"); 

我从对象如下获取数据,

List<SomeObject> obj = new ArrayList<SomeObject>(); 
obj.getItem1(); 
obj.getItem2(); 

所有单个项目的字符串。如何填充表体内容?

回答

9

尝试以下操作:

<tbody> 
#foreach($obj in $objs) 
    <tr><td>$obj.Item1</td><td>$obj.Item2</td></tr> 
#end 
<tbody> 

我假设你的列表名称objs下换上速度方面和你的SomeObject类有2场:项目1和ITEM2与coresponding干将。

List<SomeObject> objs = ... //prepopulated 
context.put("objs", objs); 

velocity documentation上查看更多内容。