This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

dataSourceResourceLoader to load velocity Template from db

 
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");
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic