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