Velocity Based Spring MVC in Sakai

29 09 2006

Well it is possible, but you have to rewrite the Spring MVC view resolver and the Velocity Configurer so that neither extend any Spring framework classes, otherwise they will want to drag Velocity into shared. Once you have done that, it works like a dream, except that you cant easily get the templates onto the filesystem.

They work just fine in the classpath, but to get a ResourceLocator to know about the filesystem it has to have a ServletContext. In a normal environment this would be easy, just do it from the request.getSession().getServletContext()…. but in Sakai, thats a Wrapped request and you cant get the servlet context that way. So the robust, but slightly complex way of doing this is to. Put a Spring bean into applicationContext.xml that can hold a ServletContext, then have a ServletContextListener that starts after the Spring ContenxtLoaderListener and injects the ServletContext into the holder. Then the holder itself can be injected into the springapp-servlet.xml as a dependency, so when the VelocityConfigurer starts up it can set the ServletContext in the instance of the Velocity Engine bound to the web app. You cant use a singleton Velocity engine, since it would be the only one in the JVM…… then…… the standard WebappResourceLoader can get gold of the ServletContext, via rsvc in ResourceLocader and hence the real path to a template on the filesystem….. lost yet 🙂

Perhaps I’ll make a jar of this as the same applies to any tightly bound SpringMVC view technology.




2 responses

7 12 2006

You could just implement the ServletContextAware interface on your bean, and Spring will call setServletContext on that bean for you. Might be a lot easier than what you describe above.

7 12 2006
Ian Boston

Thank you, that simplifies the process greatly, I’ll give it a try

%d bloggers like this: