Strictly Programming

Using Velocity in a Tapestry 5 app:

Posted in Tapestry 5 by sqllyw on 12/08/2008


1. Declare dependency in pom.xml

<dependency>
 	<groupId>velocity</groupId>
 	<artifactId>velocity</artifactId>
   	<version>1.5</version>
</dependency>  	

2. Put sample.vm file under resources directory

3. Add following code:

Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

VelocityEngine ve = new VelocityEngine();
   
ve.init(p);

try {
	Template t = ve.getTemplate("sample.vm");
    VelocityContext context = new VelocityContext();
    context.put("name", "World");
    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    System.out.println(writer.toString());
}
catch( ResourceNotFoundException rnfe ) {
  	System.out.println("cant find template");
}
catch (Exception e) {
	e.printStackTrace();      	
}

Note:
If you use IDEA to build, update the IDE setting to include *.vm as resources pattern too.(setting/compiler/resources patterns)

generate a Tapestry 5.0.18 project
mvn archetype:create -DarchetypeGroupId=org.apache.tapestry -DarchetypeArtifactId=quickstart -DgroupId=org.apache.tapestry -DartifactId=tutorial -DpackageName=org.apache.tapestry5.tutorial -DarchetypeVersion=5.0.18

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: