【问题标题】:Display a list of java objects in velocity templates as html table将速度模板中的 java 对象列表显示为 html 表
【发布时间】:2013-03-10 20:08:26
【问题描述】:

我正在从数据库中获取对象列表。我想使用速度模板将它们填充到 html 表中。

<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();

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

【问题讨论】:

    标签: java velocity


    【解决方案1】:

    尝试以下方法:

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

    我假设您的列表放在名称 objs 下的速度上下文中,并且您的 SomeObject 类有 2 个字段:item1 和 item2 以及对应的 getter。

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

    在velocity documentation 上查看更多信息。

    【讨论】:

      猜你喜欢
      • 2016-05-08
      • 1970-01-01
      • 2021-07-27
      • 2020-03-22
      • 2021-06-13
      • 2015-09-28
      • 1970-01-01
      • 2020-09-09
      • 2020-06-02
      相关资源
      最近更新 更多