views:

453

answers:

2

Hi all,

I have recently been introduced to the Velocity project.

I have written a template and have run it as a simple Java application.

Integration within my existing web project has not been that easy.

Can anyone supply a cookbook for integrating Velocity and Tomcat?

Thanks all!

+1  A: 

Tomcat is a servlet container; you don't need to integrate Velocity with it but rather with your application. How exactly that should be done depends on your application:

ChssPly76
+2  A: 

The straightforward way is to define a VelocityViewServlet in web.xml

<servlet>
 <servlet-name>view</servlet-name>
 <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
 <init-param>
   <param-name>org.apache.velocity.properties</param-name>
   <param-value>/WEB-INF/velocity.properties</param-value>
 </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>view</servlet-name>
  <url-pattern>*.vm</url-pattern>
</servlet-mapping>

In velocity.properties

#resource loaders
resource.loader = production

production.resource.loader.class = org.apache.velocity.tools.view.WebappResourceLoader

Then put your template at the root of your webapp and access it from the web browser using its name as the URL. e.g.

http://localhost:8080/index.vm
Cue