高分求助:我用Velocity为什么不能找到vm文件???

ninsky 2003-09-27 01:26:07
我用的velocity时总是提示Unable to find resource 'test.vm“!!!
我曾经用它的VelocityServlet成功地做了几个页面,也是使用它的例子中的那个command模式,这次我想作一个类可以在jsp页面中调用,可是老是出错,郁闷

我写的类中没有用其Servlet形式,而是将其封装进自己的类中,以便灵活调用:
velocity.properties:
runtime.log = /WEB-INF/conf/logs/JX_velocity.log
runtime.log.invalid.references = true
input.encoding = ISO-8859-1
output.encoding = GBK
resource.loader = file
file.resource.loader.description = Velocity File Resource Loader
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = F:/Apache~1/Tomcat~1/webapps/Jx/WEB-INF/template(原本是使用/WEB-INF/template形式,失败)

我写的测试类:
public class Test{
VelocityContext context = new VelocityContext();

protected void Init(String[] params) {

// TODO Auto-generated method stub
context.put("test1", "testtest");
context.put("test2", "hahah人民ahahah");

}

/**
* 处理模板信息,并将其做String类型返回,以便在页面输出
*
* @param params 从数据库中取数据的条件,具体内容由子类自己控制。主要供Init方法使用
* @param temporaryName 模板名称
* @return 处理后的模板信息
* @throws Exception 由于本方法主要在jsp页面中调用,故异常处理交由 jsp 处理
*/
public String paperInfo(String[] params, String temporaryName)
throws Exception {
Init(params); // 首先充实context内容

Properties prop = new Properties();
prop.load(this.getClass().getResourceAsStream("/velocity.properties"));

Velocity.init(prop); // 初始化 Velocity

StringWriter sw = new StringWriter();
Object outputEncoding = Velocity.getProperty("output.encoding");
Velocity.mergeTemplate(temporaryName,
(outputEncoding == null ? RuntimeConstants.ENCODING_DEFAULT : ((String) outputEncoding)),
context, sw);

return sw.toString();
}
}

模板文件:
${test1}-jdkfasjkfj
$test2


jsp文件:
<html>
<head>
</head>
<body>
<br>
<%PrintPaper test = new TestPrintPaper();
out.println();
String testStr = test.paperInfo(null, "test.vm");
out.println(testStr);
%>
</body>
</html>
...全文
251 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
truezerg 2003-11-11
  • 打赏
  • 举报
回复
有人知道吗?
truezerg 2003-11-10
  • 打赏
  • 举报
回复
我也找不到VM文件,楼主,我的例子比你简单多了。我刚用velocity,用的例子就是网上最常见到的例子

java类文件代码
--------------------------------------------------
import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

public class HelloWorld {
public static void main(String[] args) throws Exception {/* first, get and initialize an engine */
VelocityEngine ve = new VelocityEngine();
ve.init();

Template t = ve.getTemplate("HelloWorld.vm");/* create a context and add data */
VelocityContext context = new VelocityContext();
context.put("name", "Eiffel Qiu");
context.put("site", "http://www.eiffelqiu.com");/* now render the template into a StringWriter */
StringWriter writer = new StringWriter();
t.merge(context, writer);/* show the World */
System.out.println(writer.toString());
}
}
---------------------------------------------
vm文件HeoolWorld.vm代码
---------------------------------------------
Hello $name! Welcome to $site world!
---------------------------------------------

就是说找不到VM文件,为什么啊?

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'HelloWorld.vm'
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:501)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:384)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:814)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:796)
at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:512)
at HelloWorld.main(HelloWorld.java:11)
Exception in thread "main" Process terminated with exit code 1
ninsky 2003-09-27
  • 打赏
  • 举报
回复
不会吧,难道我的问题就这么没意思?
没人感兴趣????
miwoo 2003-09-27
  • 打赏
  • 举报
回复
不是有个VelocityViewServlet(具体记不清了,到google上查查)的Velocity引擎(类似于jsp引擎),用它就不用自己编码来处理.vm文件了,直接把.vm文件映射到这个Servlet就行了。

81,115

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧