报错:
   Template not found:org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'VarRef.vm'
源码:
   VarRef.java
-----------------------------
package velocity;
import java.io.StringWriter;import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;public class VarRef { public static void main( String[] args ){

try{
Velocity.init();
}catch( Exception x ){
System.err.println("Failed to initialize Velocity:" + x);
System.exit(1);
}

Template varTemplate = null;

try{
varTemplate = Velocity.getTemplate("VarRef.vm");
}catch( ResourceNotFoundException rnfx ){
System.out.println("Template not found:" + rnfx);
System.exit(1);
}catch( ParseErrorException peX ){
System.out.println("Failed to parse template:" + peX);
System.exit(1);
}catch( Exception x ){
System.out.println("Failed to initialize template:" + x);
System.exit(1);
}

VelocityContext context = new VelocityContext();
context.put("username", "tones");
context.put("password", "123456");

StringWriter writer = new StringWriter();

try{
varTemplate.merge(context, writer);
}catch( ResourceNotFoundException rnfX ){
System.out.println("Template not found on merge:"+rnfX);
System.exit(1);
}catch( ParseErrorException peX ){
System.out.println("Failed to parse template on merge:"+peX);
System.exit(1);
}catch( MethodInvocationException miX ){
System.out.println("Application method exception:"+miX);
System.exit(1);
}catch( Exception x ){
System.out.println("Failed to merge template:"+x);
}

System.out.println(writer.toString());

}

}
-------------------------
          VarRef.vm
-------------------------
##this is a text template
username:$username
password:$password
--------------------------
目录结构: -VelocityDemo2
   -src
      -velocity
         VarRef.java
         VarRef.vm
   +JRE Sys...
   +J2EE 1.4 ..
   -REeferenced Libraries
      velocity-1.6.4.jar
      commons-collections-3.2.1.jar
      commons-lang-2.4.jar
      commons-logging-1.1.jar
      log4j-1.2.12.jar
      oro-2.0.8.jar
   +WebRoot
   velocity.log编程环境:
   win7
   Myeclipse6.0
   jdk1.6
   velocity-1.6.4

解决方案 »

  1.   

    人家都给你说了找不到VarRef.vm。这个东西应该需要添加到你的project
      

  2.   

    你吧VarRef.vm和你的jar文件放在一起看一下看
      

  3.   

       VelocityEngine ve = new VelocityEngine() ;
    //     VelocityEngine ve = new VelocityEngine();
        Properties properties = new Properties();   
        String basePath = "D:/apache-tomcat-6.0.6/webapps/gg";   
        // 设置模板的路径   
        properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);   
        // 初始花velocity 让设置的路径生效   
        ve.init(properties);
        template = realPath + template;
        Template t = ve.getTemplate("/gg/gg1/tp_content.vm","GBK");
    把里面的路径替换成你的vm所在的路径就行了
      

  4.   

    template 和 realPath 哪儿来的?
      

  5.   


    改成这样了
    package velocity;import java.io.StringWriter;
    import java.util.Properties;import org.apache.velocity.Template;
    import org.apache.velocity.VelocityContext;
    import org.apache.velocity.app.Velocity;
    import org.apache.velocity.exception.MethodInvocationException;
    import org.apache.velocity.exception.ParseErrorException;
    import org.apache.velocity.exception.ResourceNotFoundException;public class VarRef { public static void main( String[] args ){

    try{
    Velocity.init();
    }catch( Exception x ){
    System.err.println("Failed to initialize Velocity:" + x);
    System.exit(1);
    }

    Template varTemplate = null;
    Properties properties = new Properties();   
        String basePath = "E:/root/servers/apache-tomcat-6.0.20/webapps/VelocityDemo2/WEB-INF/classes/velocity";   
        // 设置模板的路径   
        properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath); 
    try{
    varTemplate = Velocity.getTemplate("VarRef.vm");
    }catch( ResourceNotFoundException rnfx ){
    System.out.println("Template not found:" + rnfx);
    System.exit(1);
    }catch( ParseErrorException peX ){
    System.out.println("Failed to parse template:" + peX);
    System.exit(1);
    }catch( Exception x ){
    System.out.println("Failed to initialize template:" + x);
    System.exit(1);
    }

    VelocityContext context = new VelocityContext();
    context.put("username", "tones");
    context.put("password", "123456");

    StringWriter writer = new StringWriter();

    try{
    varTemplate.merge(context, writer);
    }catch( ResourceNotFoundException rnfX ){
    System.out.println("Template not found on merge:"+rnfX);
    System.exit(1);
    }catch( ParseErrorException peX ){
    System.out.println("Failed to parse template on merge:"+peX);
    System.exit(1);
    }catch( MethodInvocationException miX ){
    System.out.println("Application method exception:"+miX);
    System.exit(1);
    }catch( Exception x ){
    System.out.println("Failed to merge template:"+x);
    }

    System.out.println(writer.toString());

    }

    }
    还是报同样的错
      

  6.   

    问题了解决了
    varTemplate = Velocity.getTemplate("src/velocity/VarRef.vm");
    这样写就OK了