July 27, 2017

How to configure Apache Velocity template engine

Velocity is a open source project of the Apache Software Foundation and it defines a java-based template engine for the presentation tier in a Web application. In a MVC (model-view-controller) scenario, Velocity provides a template language to reference objects defined in Java code. Let’s see how to configure Velocity in a Struts 1.3 web application using Eclipse IDE.

CHECK OUT THE SOURCE CODE ON GITHUB

 STEP 1.

Open Eclipse and create a new web project  (File >New >Dynamic Web Project)

web-project-eclipse

STEP 2.

Download and unzip the Struts 1.3 archive : http://struts.apache.org/download.cgi

struts-unzipped

STEP 3.

Unzip the struts-blank war file located in the “apps” directory

struts-blank-unzipped

STEP4.

Copy the jar libraries in the Eclipse project

STEP 5.

Copy the struts configuration and the validator files in the Eclipse project:

STEP 6.

Download and unzip the velocity-tools archive: http://velocity.apache.org/download.cgi#Tools

STEP 7.

Copy in the WebContent/WEB-INF/lib of the Eclipse project the following velocity jar libraries:

STEP 8.

Now move into the Eclipse project and configure the following files

WebContent/WEB-INF/velocity.properties

WebContent/WEB-INF/template/layout.vm

WebContent/WEB-INF/template/header.vm

WebContent/WEB-INF/template/footer.vm

WebContent/WEB-INF/css/my.css

WebContent/WEB-INF/tools.xml

STEP 9.

Update the WebContent/WEB-INF/web.xml with the velocity servlets definition:

STEP 10.

Create a test page WebContent/velocityPage.vm:

STEP 11.

Update the WebContent/struts-config.xml

The final project layout should look like this

velocity-project-layout

Start Tomcat and go to http://localhost:8080/sample-project/testVelocity.do

 velocity-test-screen

Related posts

Leave a Reply

Your email address will not be published.