Employee.java
import java.io.StringWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class Employee
{
public static void main
( String[] args
)
{
/*
* first, get and initialize an engine
*/
VelocityEngine ve = new VelocityEngine();
ve.init();
/*
* organize our data
*/
map.put("name", "srinivas");
map.put("sal", "$10000.00");
list.add( map );
map.put("name", "umamahesh");
map.put("sal", "$59000.99");
list.add( map );
map.put("name", "ravi kumar");
map.put("sal", "$3000.99");
list.add( map );
map.put("name", "anil kumar");
map.put("sal", "$30000.99");
list.add( map );
/*
* add that list to a VelocityContext
*/
VelocityContext context = new VelocityContext();
context.put("empList", list);
/*
* get the Template
*/
Template t = ve.getTemplate( "employee_html.vm" );
/*
* now render the template into a Writer, here
* a StringWriter
*/
t.merge( context, writer );
/*
* use the output in the body of your emails
*/
System.
out.
println( writer.
toString() );
}
}
employee_html.vm
<HTML>
<HEAD>
<TITLE>Employee Details</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>$empList.size() Employees Details</B>
<BR/>
We are proud to offer these fine pets
at these amazing prices.
<BR/>
This month only, choose from :
#set( $count = 1 )
<TABLE>
#foreach( $emp in $empList )
<TR>
<TD>$count)</TD>
<TD>$emp.name</TD>
<TD>$emp.sal</TD>
</TR>
#set( $count = $count + 1 )
#end
</TABLE>
<I>Visit Again</I>
</CENTER>
</BODY>
</HTML>
Set the
class path
for velocity
-dep
-1.2.
jar and then run the application.