Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未找到Velocity框架资源异常_Java - Fatal编程技术网

Java 未找到Velocity框架资源异常

Java 未找到Velocity框架资源异常,java,Java,我试图在Java 1.6中运行velocity电子邮件发送者程序,但它找不到资源 VelocityContext context = new VelocityContext(); context.put("name", "mike"); // Initialize the engine try { VelocityEngine ve = new VelocityEngine(); templateName = "myfile_en.vm";

我试图在Java 1.6中运行velocity电子邮件发送者程序,但它找不到资源

    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);
有谁能帮我找出为什么我不能加载我的文件\u en.vm???我也尝试给出完整的绝对路径,但仍然是相同的错误:ResourceNotFound

我直接从eclipse运行它。非常感谢您的帮助


谢谢

这实际上取决于模板文件的位置。速度总是有问题的。为了解决这个问题,您需要确保模板在类路径上是可定位的。要么在jar中,要么直接在文件系统上。一旦它在类路径上,初始化速度如下

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在类路径中查找模板文件。

这实际上取决于模板文件的位置。速度总是有问题的。为了解决这个问题,您需要确保模板在类路径上是可定位的。要么在jar中,要么直接在文件系统上。一旦它在类路径上,初始化速度如下

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>

班
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

仅将路径更改为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>

班
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

文件在哪里?运行配置的起始目录是什么?文件位于哪里?运行配置的起始目录是什么?你能告诉我在哪里添加这个initVelocity()方法吗??我是否需要将其添加到同一主程序中?它将如何被调用?谢谢你的帮助!在使用velocity模板之前,需要从主方法调用它。在servlet容器中,将其放入启动servlet-启动servlet容器时由servlet容器调用的servlet。我正在使用一个独立的java程序……您能告诉我应该在哪里调用它以及如何调用它吗?Mike,首先将模板放在类路径中的文件夹中,然后调用initVelocity()方法,然后再使用模板。。意思是…从主方法中,首先调用initVelocity(),然后继续说VelocityContext ctx=new VelocityContext(),然后继续。。在使用Velocity类之前,只需调用一次initVelocity()。您能告诉我在哪里添加此initVelocity()方法吗??我是否需要将其添加到同一主程序中?它将如何被调用?谢谢你的帮助!在使用velocity模板之前,需要从主方法调用它。在servlet容器中,将其放入启动servlet-启动servlet容器时由servlet容器调用的servlet。我正在使用一个独立的java程序……您能告诉我应该在哪里调用它以及如何调用它吗?Mike,首先将模板放在类路径中的文件夹中,然后调用initVelocity()方法,然后再使用模板。。意思是…从主方法中,首先调用initVelocity(),然后继续说VelocityContext ctx=new VelocityContext(),然后继续。。在使用Velocity类之前,只需调用一次initVelocity()。