How to use Apache Velocity in Java
Posted By : Gursahib Singh | 30-Jul-2019
Apache velocity is a Java-based template engine. It permits everyone to use a simple yet powerful template language to reference objects defined in Java code. Velocity templates are used to separate java code from the web pages, making the web pages more maintainable over its lifespan and providing a possible alternative to Java Server Pages (JSPs) or PHP.
Apache velocity is a project of the Apache Software Foundation, charged with the creation and maintenance of open-source software product related to the apache velocity template engine. All software created using the velocity template is available under the Apache Software License and free of charge for the public.
Apache Velocity Projects
Apache Velocity offers the following projects:
- Velocity Engine : This is the actual velocity templating engine which does all the work. If you came here because you heard about velocity template some where on the web, this is probably the right place to start the project.
- Velocity Tools : This project contains tools and other useful infrastructure to build web and non-web application using the velocity template engine. You will find e.g. code for Struts application integration or the standalone VelocityViewServlet here.
Where do we use Apache velocity?
Apache Velocity can be successfully used in:
- Java servlet-based web applications.
- In java and SQL code generation.
- XML processing, and XML transformation.
- Text processing, such as RTF file generation and many more.
Example with java
Any application using Velocity requires two parts. The first is the template, which in this example is a file called helloVelocity.vm
The second is a corresponding Java program called HelloVelocity.java:
import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
public class HelloVelocity
{
public static void main( String[] args )
throws Exception
{
VelocityEngine ve = new VelocityEngine();
ve.init();
Template t = ve.getTemplate( "helloworld.vm" );
VelocityContext context = new VelocityContext();
context.put("name", "Oodles");
StringWriter writer = new StringWriter();
t.merge( context, writer );
System.out.println( writer.toString() );
}
}
Prerequisites
- Java 1.7
- Apache Velocity Jar
For Apache Velocity JAR file add the following dependency in your class path :
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Gursahib Singh
Gursahib is a software developer having key skills in J2SE and J2EE. His hobbies are playing chess, reading and learning new softwares.