高分求助:我用Velocity为什么不能找到vm文件???
我用的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>