1

I have added Apache Velocity 1.7 to my spring 3.2.5.RELEASE application in order to convert html to string and send mail. My spring context is defined below:

<bean id="velocityEngine1" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <props>
            <prop key="resource.loader">class</prop>
            <prop key="class.resource.loader.class">
                org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
            </prop>
        </props>
    </property>
</bean>

I have added the file test.vm in my src/main/resources folder.

The below line is where I'm using the engine:

   @Autowired
    @Qualifier("velocityEngine1")
    private VelocityEngine velocityEngine;

public JSONResult uploadFile(MultipartFile file, AppUserDTO appUserDTO){

        String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "test", "UTF-8", null);
        System.out.println(body);

...

}

When it execute the method VelocityEngineUtils.mergeTemplateIntoString I get exception:

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'test'
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
user1999453
  • 1,297
  • 4
  • 29
  • 65

1 Answers1

2

You need to put full path with template file, in your case

 VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "test.vm", "UTF-8", null);
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • in case if i have my test.vm in an additional folder such as src/main/resources/template/ do i need to update my bean for the location? – user1999453 Sep 24 '19 at 12:07
  • 1
    @user1999453 No, Just use `"/template/test.vm"` when calling `mergeTemplateIntoString` – Ori Marko Sep 24 '19 at 12:08