Velocity 2.0 无法在 jar 中找到模板资源
Velocity 2.0 unable to find template resource in jar
我在 Spring Boot 5 中使用 Velocity 2.0 发送电子邮件。我从 src/main/resources/email/ 加载模板并在 config.xml 文件
中将 VelocityEngine 定义为 bean
<bean id="velocityEngine" class="org.apache.velocity.app.VelocityEngine">
<property name="properties">
<props>
<prop key="resource.loader">file</prop>
<prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader</prop>
<prop key="file.resource.loader.path">src/main/resources/templates/email/</prop>
<prop key="file.resource.loader.cache">true</prop>
<prop key="file.resource.loader.modificationCheckInterval">5</prop>
</props>
</property>
</bean>
当我从 Intellij IDEA 运行 时,一切正常,但是当我在服务器上构建 jar 到 运行 时,它抛出异常:
org.apache.velocity.exception.ResourceNotFoundException: 无法找到资源 'MyTemplate.vm'。知道如何解决吗?如有任何帮助,我将不胜感激
因此,您正在使用 FileResourceLoader 并为其指定一个相对路径 "src/main/resources/templates/email/"
因此,除非与您的应用程序运行位置相关的路径在服务器上,否则它会给您一个完全合理的错误代码。
您想使用 bean 配置上的 ClasspathResourceLoader 做什么,并确保您的模板正确打包在生成的 jar 中。
我在 Spring Boot 5 中使用 Velocity 2.0 发送电子邮件。我从 src/main/resources/email/ 加载模板并在 config.xml 文件
中将 VelocityEngine 定义为 bean<bean id="velocityEngine" class="org.apache.velocity.app.VelocityEngine">
<property name="properties">
<props>
<prop key="resource.loader">file</prop>
<prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader</prop>
<prop key="file.resource.loader.path">src/main/resources/templates/email/</prop>
<prop key="file.resource.loader.cache">true</prop>
<prop key="file.resource.loader.modificationCheckInterval">5</prop>
</props>
</property>
</bean>
当我从 Intellij IDEA 运行 时,一切正常,但是当我在服务器上构建 jar 到 运行 时,它抛出异常: org.apache.velocity.exception.ResourceNotFoundException: 无法找到资源 'MyTemplate.vm'。知道如何解决吗?如有任何帮助,我将不胜感激
因此,您正在使用 FileResourceLoader 并为其指定一个相对路径 "src/main/resources/templates/email/"
因此,除非与您的应用程序运行位置相关的路径在服务器上,否则它会给您一个完全合理的错误代码。
您想使用 bean 配置上的 ClasspathResourceLoader 做什么,并确保您的模板正确打包在生成的 jar 中。