前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >velocity调用java静态方法_java模板引擎

velocity调用java静态方法_java模板引擎

作者头像
全栈程序员站长
发布2022-11-01 16:15:35
2.1K0
发布2022-11-01 16:15:35
举报
文章被收录于专栏:全栈程序员必看

跟学习其它技术一样,首先到官网去下载必要的包,下载地址:http://velocity.apache.org/download.cgi目前使用的是velocity 1.6.3,由于自己的E文水平一般,在使用之前也到网上搜索了相关文章,然后根据前辈们的指导和自己的实践结合.记录下此文,以便以后能快速回忆.

一、在eclipse 中新建一个工程,把包velocity-1.6.3.jar到在WEB-INF/lib下,

二、新建了一个hello.vm的测式模板

Html代码

b212d5639dc6fc4159eb130fa6142ffa.png
b212d5639dc6fc4159eb130fa6142ffa.png

HELLO! $name,Welcome to velocity!

三、新建一个java属性文件 velocity.properties,参考了别人的配置示例,详细的说明以后再理解

Java代码

b212d5639dc6fc4159eb130fa6142ffa.png
b212d5639dc6fc4159eb130fa6142ffa.png

#Velocity.properties配置示例

# 如果需要系统从WEB-INF/classes路径加载Velocity的模板文件,取消下面两行的注释

#resource.loader=class

#class.resource.loader.class=org.apache.Velocity.runtime.resource.loader.ClasspathResourceLoader

#如需禁止系统通过文件系统加载模板文件,注释如下两行

resource.loader=file

file.resource.loader.path=D:\Workspaces\MyEclipse 8.5\velocity\WebRoot\WEB-INF\velocityTempalte

#确定从何处加载velocity的模板文件

file.resource.loader.cache=false

#设置读取模板文件的解码格式,GB2312是为了支持中文

input.encoding=gb2312

#配置输出视图文件的解码格式,GB2312是为了支持中文

output.encoding=gb2312

四、新建一个测式类VelocityTest.java

Java代码

b212d5639dc6fc4159eb130fa6142ffa.png
b212d5639dc6fc4159eb130fa6142ffa.png

package velocity.test;

import java.io.FileOutputStream;

import java.io.PrintWriter;

import java.io.StringWriter;

import org.apache.velocity.Template;

import org.apache.velocity.VelocityContext;

import org.apache.velocity.app.Velocity;

public class VelocityTest {

public static void main(String[] args) {

try {

// 初始化

Velocity.init(“D:\\Workspaces\\MyEclipse 8.5\\velocity\\WebRoot\\WEB-INF\\velocity.properties”);

//取得velocity上下文

VelocityContext context = new VelocityContext();

context.put(“name”, “sea”);

Template template = Velocity.getTemplate(“hello.vm”);

StringWriter writer = new StringWriter();

template.merge(context, writer);

PrintWriter filewriter = new PrintWriter(new FileOutputStream(“d:\\a.html”),true);

filewriter.println(writer.toString());

filewriter.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

最后运行该类时出现如下错误:

Java代码

b212d5639dc6fc4159eb130fa6142ffa.png
b212d5639dc6fc4159eb130fa6142ffa.png

Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/collections/ExtendedProperties

发现缺少了集合包,把velicity.1.6.3解压后的lib目录下的commons-collections-3.2.1.jar拷入工程的lib目录下,继续运行,还是出现以后错误:

Java代码

b212d5639dc6fc4159eb130fa6142ffa.png
b212d5639dc6fc4159eb130fa6142ffa.png

org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.ServletLogChute with the current runtime configuration.

at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:206)

at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:255)

at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:795)

at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:250)

at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:615)

at org.apache.velocity.runtime.RuntimeSingleton.init(RuntimeSingleton.java:243)

at org.apache.velocity.app.Velocity.init(Velocity.java:93)

at velocity.test.VelocityTest.main(VelocityTest.java:17)

Caused by: java.lang.UnsupportedOperationException: Could not retrieve ServletContext from application attributes

at org.apache.velocity.runtime.log.ServletLogChute.init(ServletLogChute.java:73)

at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:157)

… 7 more

最后分别把 commons-lang-2.4.jar和commons-logging-1.1.jar拷入lib目录才正常

输出d:\a.html下的文件如下

Java代码

b212d5639dc6fc4159eb130fa6142ffa.png
b212d5639dc6fc4159eb130fa6142ffa.png

HELLO! sea,Welcome to velocity!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/179682.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月20日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档