Sunday, October 2, 2011

Setting up Scalate with Spring MVC

A week back I tried out using scalate with spring mvc:
The above link talks about just the spring configuration entry. I thought I could add a bit more information.
Get the required libraries
Yeah, this must be obvious. But just to keep things explicit.
  • scalate libraries  - scalate-core-1.5.2.jar, scalate-util-1.5.2.jar (I did not know I need the util jar also till I hit an issue) 
  • scalate spring mvc integration library - scalate-spring-mvc-1.5.2.jar
  • scala jars (hmmm... now that was obvious but yet...) - scala-library-2.9.1.jar, scala-compiler-2.9.1.jar.  Still not sure why the compiler is required!!!
Configure the spring configuration file
I think this is already covered in the above link. It tries to configure a view resolver for scalate templates.


A sample Spring MVC controller
A sample controller which provides some data to the template (I used the mustache based templates which we will see soon).
@Controller
public class ScalatePOCController {

	@RequestMapping("/scalatePOC.html")
	public ModelAndView pocHandler() {
		ModelAndView modelAndView = new ModelAndView();

		String name = "Superman";
		Product product = new Product();
		product.setId(10l);
		product.setName("Super Belt");
		return modelAndView;
	}

}

A sample template
I used mustache as my templating mechanism. Kind of like the fact that I can use the same template on both server and client side.
Hello Basic POC working!!!
Hello {{name}}!

This is the Product you want!!!
	{{#product}}
		{{id}}
		{{name}}
	{{/product}}


That I thought should get me rolling. When I then ran my tomcat to see the page, I got OOM (OutOfMemoryError). The JVM perm gen space had run out. Looks like all the scala stuff meant I needed a little more memory (could be just my case because I had other stuff also in my project.... so may not happen to all). I increased my perm gen space by giving the following jvm argument.
-XX:MaxPermSize=256M
That did it! I got my simple scalate - spring mvc app running...

No comments: