<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Another Internet Matt &#187; spring</title>
	<atom:link href="http://www.mjohnston.com/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mjohnston.com</link>
	<description>Making the internet a better place through open source</description>
	<lastBuildDate>Sat, 23 Jan 2010 21:48:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Jackrabbit to Store Velocity Templates in Spring</title>
		<link>http://www.mjohnston.com/2008/11/using-jackrabbit-to-store-velocity-templates-in-spring/</link>
		<comments>http://www.mjohnston.com/2008/11/using-jackrabbit-to-store-velocity-templates-in-spring/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 19:28:48 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jackrabbit]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[velocity]]></category>

		<guid isPermaLink="false">http://www.mjohnston.com/?p=28</guid>
		<description><![CDATA[I love Velocity. It is simple and quick to pickup. Plus, it plays nicely into MVC design patterns. On a recent project, I wanted to use velocity templates as my views in Spring MVC. Setting that up is pretty straight forward. You just need to add a few entries to your ***-servlet.xml

&#60;bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"&#62;
&#60;property name="resourceLoaderPath" value="/" [...]]]></description>
			<content:encoded><![CDATA[<p>I love <a href="http://velocity.apache.org">Velocity</a>. It is simple and quick to pickup. Plus, it plays nicely into MVC design patterns. On a recent project, I wanted to use velocity templates as my views in Spring MVC. Setting that up is pretty straight forward. You just need to add a few entries to your ***-servlet.xml</p>
<pre language="xml">
&lt;bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"&gt;
&lt;property name="resourceLoaderPath" value="/" /&gt;
&lt;/bean&gt;

&lt;bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"&gt;
&lt;property name="cache" value="true" /&gt;
&lt;property name="prefix" value="" /&gt;
&lt;property name="suffix" value=".vm" /&gt;
&lt;property name="exposeSpringMacroHelpers" value="false" /&gt;
&lt;/bean&gt;</pre>
<p>The velocityConfig bean initializes the velocity engine and sets any velocity specific properties. The viewResolver bean tells spring to use velocity as the view layer instead of the default jsp.</p>
<p>So it&#8217;s that simple to get velocity working in Spring. On my current project, I am using <a href="http://jackrabbit.apache.org">Apache Jackrabbit</a> to store content. I started thinking, why not store my velocity templates in Jackrabbit also? It may be a bit of a philosophical reason on where to store your templates, but for this project I wanted to limit the amount of server access needed by the site administrators. So by storing the templates in Jackrabbit, or a database, I can allow the templates to be modified through web forms. Again, this decision is more philosophical and not really the intent of this posting.</p>
<p>Back to the main topic, how to store your velocity templates in Jackrabbit and then have Spring use those templates. This is actually really simple to do. First, you need to get your Spring application setup to handle Jackrabbit. I did this by following the instructions to setup the springmodules-jcr module of <a href="https://springmodules.dev.java.net/">Spring Modules</a>. Once you setup springmodules-jcr, you will most likely have a spring bean that will interact with Jackrabbit. For this example, I will call that bean &#8220;jcrService&#8221;.</p>
<p>We are going to use the ResourceLoader feature of velocity. The ResourceLoader was built to do exactly what we are doing. It allows you to override where and how your templates files are stored. Some existing ResourceLoaders include DataSourceResourceLoader, JarResourceLoader and URLResourceLoader. We need to make a JcrResourceLoader.</p>
<p>Here is the code for my JcrResourceLoader:</p>
<pre LANGUAGE="java">
import java.io.InputStream;
import javax.jcr.Node;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;</code>

public class JcrResourceLoader extends ResourceLoader {

private JcrService jcrService;

public JcrService getJcrService() {
return jcrService;
}

public void setJcrService(JcrService jcrService) {
this.jcrService = jcrService;
}

@Override
public InputStream getResourceStream(String name)
throws ResourceNotFoundException {

try {
InputStream ins = null;
Node node = jcrService.getNode(name);
Node content = node.getNode("jcr:content");
if (content.hasProperty("jcr:data")) {
ins = content.getProperty("jcr:data").getStream();
}
return ins;

}
catch (Exception e) {
log.error("could not load template for path: " + name);
return null;
}
}

}
</pre>
<p>Now we need to tell our velocity configuration about this resource loader. In our ***-servlet.xml file, we need to create a bean for our resource loader and to change our velocity config parameters.</p>
<pre language="xml">
&lt;bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"&gt;
&lt;property name="resourceLoaderPath" value="/" /&gt;
&lt;property name="velocityPropertiesMap"&gt;
&lt;map&gt;
&lt;entry key="resource.loader" value="jcr" /&gt;
&lt;entry key="jcr.resource.loader.instance" value-ref="jcrResourceLoader" /&gt;
&lt;/map&gt;
&lt;/property&gt;

&lt;/bean&gt;

&lt;bean id="jcrResourceLoader" class="JcrResourceLoader" &gt;
&lt;property name="jcrService" ref="jcrService"/&gt;
&lt;/bean&gt;
</pre>
<p>And that&#8217;s it. Now when your spring controller goes tries to load the velocity template, it will use the JcrResourceLoader to lookup and load the velocity template. This code is just a first prototype and will need to be cleaned up for error checking and performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mjohnston.com/2008/11/using-jackrabbit-to-store-velocity-templates-in-spring/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

