有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法找到资源,尽管文件位于类路径中

我已经将ApacheVelocity1.7添加到我的Spring3.2.5中。发布应用程序以将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>

我已经添加了文件测试。我的src/main/resources文件夹中的vm

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

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

共 (1) 个答案

  1. # 1 楼答案

    你需要把full path with template file放在你的例子中

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