报错:
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
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
解决方案 »
- java.lang.OutOfMemoryError: unable to create new native thread这个异常怎么解决?
- 引用org.springframework.orm出错,一直是找不到这个包
- 无比的迷茫。。
- proxyTargetClass问题
- 一个去嵌套javabean的问题
- 通过zip方式安装,tomcat的admin和manager密码是啥子啊?
- 请问,怎样可以将JAVA文件编译成可执行文件?
- jboss的Interest例子中,提示failed to lookup java:comp/env/ejb/Interest
- 关于ejb的配置和使用(你是真正的高手吗?)
- 小白请求大牛,mybatis报错
- COOKIE传值问题!!!!
- Mysql数据库有to_date()方法吗?
// 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所在的路径就行了
改成这样了
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());
}
}
还是报同样的错
varTemplate = Velocity.getTemplate("src/velocity/VarRef.vm");
这样写就OK了