File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
Author

dataSourceResourceLoader to load velocity Template from db

RanjeetK
Greenhorn

Joined: Sep 18, 2008
Posts: 1
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");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dataSourceResourceLoader to load velocity Template from db
 
Similar Threads
Problems while Configuring Tiles in Spring MVC
Transaction ManageMent in Spring with Hibernate
best approach for a Spring MVC implemenation
Problem in injecting interface
Hibernate with Spring