繁体   English   中英

找不到速度框架资源异常

[英]Velocity framework resource not found exception

我正在尝试在Java 1.6中运行速度电子邮件发件人程序,但找不到资源...

    VelocityContext context = new VelocityContext();
    context.put("name", "mike");

    // Initialize the engine
    try {
    VelocityEngine ve = new VelocityEngine();
    templateName = "myfile_en.vm";

    // Load the template
    Template template = ve.getTemplate(templateName, "UTF-8");

    // Render the template into a writer
    StringWriter writer = new StringWriter();
    template.merge(context, writer);

Cam可以帮助我找出为什么我无法加载myfile_en.vm的原因??? 我也尝试给出完整的绝对路径,但仍然存在相同的错误:ResourceNotFound

我直接从Eclipse运行它。 任何帮助深表感谢。

谢谢!

这实际上取决于模板文件的位置。 速度总是有问题的。 为了解决这个问题,您需要确保模板在您的类路径中可定位。 可以直接放在jar或文件系统中。 一旦到达您的类路径,就可以像这样初始化init Velocity ...

private static void initVelocity() throws Exception {
    java.util.Properties p = new java.util.Properties();
    p.setProperty("resource.loader", "class");
    p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    Velocity.init(p);
}

这告诉Velocity在类路径中查找模板文件。

只将路径更改为bean

<bean id="velocityEngine" 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM