尽管文件在类路径中,但无法找到资源

Unable to find resource although file is in classpath

我已将 Apache Velocity 1.7 添加到我的 spring 3.2.5.RELEASE 应用程序中,以便将 html 转换为字符串并发送邮件。我的 spring 上下文定义如下:

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

我已将文件 test.vm 添加到我的 src/main/resources 文件夹中。

下面一行是我使用引擎的地方:

   @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);

...

}

当它执行方法时 VelocityEngineUtils.mergeTemplateIntoString 我得到异常:

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'test'

您需要输入 full path with template file,在您的情况下

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