Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dataSourceResourceLoader to load velocity Template from db

 
RanjeetK
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

storefront-servlet.xml
===============

<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 -->
</bean>

<!-- spring configuraiton-->
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean" singleton="true">
<property name="velocityProperties">
<props>
<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>
</props>
</property>

</bean>


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);
ve.init();
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());
//vTemplate.getResourceLoader();
VelocityEngineUtils.mergeTemplateIntoString(ve, "test.vm", model);

return new ModelAndView("test");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic