简体   繁体   English

找不到速度框架资源异常

[英]Velocity framework resource not found exception

I'm trying to run velocity email sender program in Java 1.6 but its not finding recourse... 我正在尝试在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 any one help me out why I'm not able to load myfile_en.vm??? Cam可以帮助我找出为什么我无法加载myfile_en.vm的原因??? I tried giving full absolute path as well but still same error: ResourceNotFound 我也尝试给出完整的绝对路径,但仍然存在相同的错误:ResourceNotFound

I'm running it directly from eclipse. 我直接从Eclipse运行它。 Any help is much appreciated. 任何帮助深表感谢。

Thanks! 谢谢!

It really depends on where your template file is. 这实际上取决于模板文件的位置。 Velocity always has an issue with it. 速度总是有问题的。 To get around it you need to ensure that the templates are locatable on your classpath. 为了解决这个问题,您需要确保模板在您的类路径中可定位。 Either in a jar or on the filesystem directly. 可以直接放在jar或文件系统中。 Once its on your classpath, init Velocity like this... 一旦到达您的类路径,就可以像这样初始化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);
}

This tells Velocity to look for template files in the classpath. 这告诉Velocity在类路径中查找模板文件。

Only change the path into bean 只将路径更改为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