Velocity - How to iterate a Map/List and format Date and Numbers:
For our example, lets take the student class as our model object.
Velocity - Iterate a List :
List<Student> studentList = new ArrayList<Student>(); Date date = new GregorianCalendar().getTime(); studentList.add( new Student("Amitabh", "Bachhan", 96.4563, date ) ); studentList.add( new Student("John", "Smith", 89.456, date ) ); studentList.add( new Student("Leonardo", "Decaprio", 65.34244, new GregorianCalendar().getTime() ) ); studentList.add( new Student("Kate", "Winslet", 55.90744, new GregorianCalendar().getTime() ) );
#foreach ($student in $studentList)
<tr>
<td>$stu.firstName</td>
<td>$stu.lastName</td>
<td>$number.format("#0.00",$stu.marksPercentage)</font></td>
<td>$date.format('short',$stu.datEnrolled)</font></td>
</tr>
#end
Velocity - Iterate a Map:
Map<Integer,Student> studentMap = new HashMap<Integer,Student>(); studentMap.put(1, new Student("Amitabh", "Bachhan", 96.4563, date )); studentMap.put(2, new Student("John", "Smith", 89.456, date ) ); studentMap.put(3, new Student("Leonardo", "Decaprio", 65.34244, new GregorianCalendar().getTime() ));
#set ($map = $studentMap )
#foreach ($mapEntry in $map.entrySet())
<tr>
<td>Student ID : $mapEntry.key</td>
<td>$mapEntry.value.firstName</td>
<td>$mapEntry.value.lastName</td>
<td>$mapEntry.value.marksPercentage</td>
<td>$mapEntry.value.datEnrolled </td>
</tr>
#end
Velocity - Format Date:
$date.format('short',$stu.datEnrolled)
Velocity - Format Number:
$number.format("#0.00",$stu.marksPercentage)
Velocity if else condition:
#if ($mapEntry.key == 1) <h2>You are a top student!</h2> #else <h2>You are not a top student!</h2> #end
