I am fetching a list of objects from DB. I would like to populate them in to a html table using velocity templates.
<table>
<thead>
<tr>
<td>$value1 </td>
<td>$value2 </td>
</tr>
</thead>
<tbody>
<!-- Iterate through the list (List<SomeObject>) and display them here, -->
</tbody>
</table>
For headers I am using the below code,
VelocityContext context = new VelocityContext();
context.put("value1", "text1");
context.put("value2", "text2");
I get data from objects as below,
List<SomeObject> obj = new ArrayList<SomeObject>();
obj.getItem1();
obj.getItem2();
All the individual items are Strings. How to populate the table body content?
Try the following:
<tbody>
#foreach( $obj in $objs )
<tr><td>$obj.Item1</td><td>$obj.Item2</td></tr>
#end
<tbody>
I assume that your list is put on the velocity context under the name objs
and your SomeObject
class has 2 fields: item1 and item2 with coresponding getters.
List<SomeObject> objs = ... //prepopulated
context.put("objs", objs);
See more on the velocity documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With