报错:
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
解决方案 »
- 求助:org.hibernate.MappingException: Unknown entity:XXX
- java发送邮件出现问题
- [struts1.x]如果配置文件中不配置mapping,在action中处理完怎么能直接跳转到指定页面?比如www.163.com
- 刚刚做了一个struts+hibernate+spring,速度不理想!!!
- ● 请问:gmail中,网页里嵌入聊天窗口是如何实现的?
- struts中session丢失
- 题目
- middlegen-hibernate配置问题 高手帮忙 (没分了 见谅 )
- 刚学使用Eclispe,遇到一个小问题
- 如果一个项目,一开始直接访问的是servlet页面,那么自动进入doPost()方法?
- 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了