Sunday, January 20, 2013

Using Spring with Velocity and SiteMesh on Google App Engine

When working with web applications, it's often important to pick a view technology that you like but also has long term support. The following will demonstrate how to integrate Velocity and SiteMesh into Spring and Google App Engine.

Tools:

  • Spring MVC 3.2.0
  • Google App Engine SDK 1.7.4
  • velocity-1.7
  • SiteMesh 2.4.2


We will be using Apache Velocity since Spring recommends it (Alternatively, you can use FreeMarker). We will also use SiteMesh because the decorator pattern to plug in header, footer, or other components are very convenient.

This tutorial will assume you have set up Spring on Google App Engine already. Read Running Spring 3.2 on Google App Engine for more details.

First download the following:



Add the following JARs to /war/WEB-INF/lib/

  • velocity-1.7.jar
  • velocity-1.7-dep.jar
  • sitemesh-2.4.2.jar
  • velocity-tools-view-2.0.jar
  • commons-collections-3.2.jar
  • commons-digester-2.1.jar
You may need to find some of the above elsewhere.

Add all these JARs to your build path.( Right Click on root project folder. Click Properties. Click Java Build Path. Click Libraries. Click Add JARs.)

In /war/WEB-INF/web.xml, add the following:

In your serlvet.xml, replace your viewResolver with:

Create the file /war/WEB-INF/sitemesh.xml and add

Create the file /war/WEB-INF/decorators.xml and add

Create the layout file /war/decorators/layout.vm and add

Create a velocity file /war/velocity/hello.vm and put the text "Hello World" into it.

Create an empty file /war/WEB-INF/velocity.properties. If you don't do this, you may get an exception saying access denied for reading velocity.properties.

Create a controller called HelloController
public class VideoController {
@RequestMapping(value="/hello", method = RequestMethod.GET)
public ModelAndView sayHello() {
return new ModelAndView("hello");
}
}

Start the server and browse http://localhost:8888/hello and check if you get the decorator content wrapper the Hello World message.


No comments:

Post a Comment