Velocity Template Example in Java
Velocity is a simple to use, open source web framework designed to be used as
the view component of an MVC architecture.
..
The main advantage of using Velocity over JSP is that Velocity is simple to use. The Velocity Template Language (VTL) is so constrained in its capabilities that it helps to enforce separation of business logic from the view. You won't see any Java classes in a Velocity template (an HTML document with some Velocity placeholders).
.
Add Three Jars : apache-commons-lang.jar , apache-velocity-velocity-1.5.jar , org.apache.commons.collections.jar
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jaxb;
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;
/**
*
* @author ananddw
*/
public class EmailDemo {
public static void main(String[] args)
throws Exception {
/*
* first, get and initialize an engine
*/
VelocityEngine ve = new VelocityEngine();
ve.init();
/*
* organize our data
*/
ArrayList list = new ArrayList();
Map map = new HashMap();
map.put("name", "Anand");
map.put("salary", "100.00");
list.add(map);
map = new HashMap();
map.put("name", "Antrish");
map.put("salary", "600.40");
list.add(map);
map = new HashMap();
map.put("name", "Ajeet");
map.put("salary", "500.00");
list.add(map);
/*
* add that list to a VelocityContext
*/
VelocityContext context = new VelocityContext();
context.put("emplist", list);
/*
* get the Template
*/
Template t = ve.getTemplate("test_html.vm");
/*
* now render the template into a Writer, here
* a StringWriter
*/
StringWriter writer = new StringWriter();
t.merge(context, writer);
/*
* use the output in the body of your emails
*/
System.out.println(writer.toString());
}
}
==============test_html.vm=====
<HTML>
<HEAD>
<TITLE>Employee details</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>$emplist.size() Employee</B>
<BR/>
This is an email generated by velocity
<BR/>
This month only, choose from :
#set( $count = 1 )
<TABLE>
#foreach( $list in $emplist )
<TR>
<TD>$count</TD>
<TD>$list.name</TD>
<TD>$list.salary</TD>
</TR>
#set( $count = $count + 1 )
#end
</TABLE>
</CENTER>
</BODY>
</HTML>
..
The main advantage of using Velocity over JSP is that Velocity is simple to use. The Velocity Template Language (VTL) is so constrained in its capabilities that it helps to enforce separation of business logic from the view. You won't see any Java classes in a Velocity template (an HTML document with some Velocity placeholders).
.
Add Three Jars : apache-commons-lang.jar , apache-velocity-velocity-1.5.jar , org.apache.commons.collections.jar
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jaxb;
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;
/**
*
* @author ananddw
*/
public class EmailDemo {
public static void main(String[] args)
throws Exception {
/*
* first, get and initialize an engine
*/
VelocityEngine ve = new VelocityEngine();
ve.init();
/*
* organize our data
*/
ArrayList list = new ArrayList();
Map map = new HashMap();
map.put("name", "Anand");
map.put("salary", "100.00");
list.add(map);
map = new HashMap();
map.put("name", "Antrish");
map.put("salary", "600.40");
list.add(map);
map = new HashMap();
map.put("name", "Ajeet");
map.put("salary", "500.00");
list.add(map);
/*
* add that list to a VelocityContext
*/
VelocityContext context = new VelocityContext();
context.put("emplist", list);
/*
* get the Template
*/
Template t = ve.getTemplate("test_html.vm");
/*
* now render the template into a Writer, here
* a StringWriter
*/
StringWriter writer = new StringWriter();
t.merge(context, writer);
/*
* use the output in the body of your emails
*/
System.out.println(writer.toString());
}
}
==============test_html.vm=====
<HTML>
<HEAD>
<TITLE>Employee details</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>$emplist.size() Employee</B>
<BR/>
This is an email generated by velocity
<BR/>
This month only, choose from :
#set( $count = 1 )
<TABLE>
#foreach( $list in $emplist )
<TR>
<TD>$count</TD>
<TD>$list.name</TD>
<TD>$list.salary</TD>
</TR>
#set( $count = $count + 1 )
#end
</TABLE>
</CENTER>
</BODY>
</HTML>
Comments
Post a Comment