Two Laptop Bag
The moose likes Other Application Frameworks and the fly likes dataSourceResourceLoader to load velocity Template from db Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "dataSourceResourceLoader to load velocity Template from db" Watch "dataSourceResourceLoader to load velocity Template from db" New topic

dataSourceResourceLoader to load velocity Template from db


Joined: Sep 18, 2008
Posts: 1

In my application there is a scenario, we need the load the velocity template from database and file system as well. Some velocity template going to load from file system and some from database using dataSourceResourceLoader. we configured DataSourceResourceLoade, velocityEngine as follows, and use the VelocityEngine in Controller like below. Template are loading but when we set the name of view in ModelView object in controller class then it's goign to search from the file system, which is not available in file system, it's loaded from database. How can I mention in velocityCofig bean id to search from the loaded template from database?. is it possible to load the template from db and from file both in one module? I am using spring-1.0 version, with spring-webflow-1.0, hibernate3. Please help me out of this problem.


<bean id="dataSourceResourceLoader" class="org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader" singleton="true">
<property name="dataSource" ref="dataSource"/> <!-- bean id of "org.apache.commons.dbcp.BasicDataSource" copied from shared-data.xml -->

<!-- spring configuraiton-->
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" singleton="true">
<property name="velocityProperties">
<prop key="resource.loader">ds</prop>
<prop key="ds.resource.loader.class">org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader</prop>
<prop key="ds.resource.loader.resource.table">s_template</prop>
<prop key="ds.resource.loader.resource.keycolumn">templateid</prop>
<prop key="ds.resource.loader.resource.templatecolumn">body</prop>
<prop key="ds.resource.loader.resource.timestampcolumn">template_timestamp</prop>
<prop key="ds.resource.loader.resource.datasource">java:comp/env/jdbc/XEC</prop>


sample code from controller class

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

VelocityEngine ve = (VelocityEngine)getWebApplicationContext().getBean("velocityEngine");
DataSourceResourceLoader ds = (DataSourceResourceLoader)getWebApplicationContext().getBean("dataSourceResourceLoader");
ve.setProperty("ds.resource.loader.instance", ds);
vTemplate = ve.getTemplate("test.vm");
String s = vTemplate.getName();
StringWriter sw = new StringWriter();
VelocityContext velocityContext = new VelocityContext(model);
org.apache.velocity.Template vTemplate.merge(velocityContext, sw);

System.out.println("text of loaded template : " +sw.toString());
VelocityEngineUtils.mergeTemplateIntoString(ve, "test.vm", model);

return new ModelAndView("test");
I agree. Here's the link:
subject: dataSourceResourceLoader to load velocity Template from db
It's not a secret anymore!