javaspringapache-velocity

Unable to find resource although file is in classpath


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'

Solution

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

     VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "test.vm", "UTF-8", null);