Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a list of java objects in velocity templates as html table

Tags:

java

velocity

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?

like image 456
NEO Avatar asked Oct 18 '25 19:10

NEO


1 Answers

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.

like image 60
dcernahoschi Avatar answered Oct 20 '25 09:10

dcernahoschi