Velocity之Hello World(入门实例)
参考:http://blog.csdn.net/mengxuwq/article/details/1871161 (非常感谢这篇文章,让我初步入门)
自己调试完全能运行后,写在此,供新人参考,供自己温故
1、准备模板文件
//如下为hellosite.vm
Hello $name! Welcome to $site world!
2、准备Java文件 (补充说明:hellosite.vm、HelloWorld.java放在同一目录下)
//如下为HelloWorld.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(); /* next, get the Template */ Template t = ve.getTemplate( "hellosite.vm" ); /* create a context and add data */ VelocityContext context = new VelocityContext(); context.put("name", "Eiffel Qiu"); context.put("site", "http://www.j2medev.com"); /* now render the template into a StringWriter */ StringWriter writer = new StringWriter(); t.merge( context, writer ); /* show the World */ System.out.println( writer.toString() ); } }
3、配置Velocity所需要的jar包(本文的调试基于 velocity-1.6.3)
3.1 下载jar包
http://velocity.apache.org/download.cgi ——>Older releases ——>最终到达 http://archive.apache.org/dist/velocity/engine/1.6.3/,找到 velocity-1.6.3.zip 下载
3.1 解压velocity-1.6.3.zip ,发现解压后的目录下存在velocity-1.6.3.jar、velocity-1.6.3-dep.jar
3.2 配置classpath环境变量,引入velocity-1.6.3.jar、velocity-1.6.3-dep.jar
4、编译运行上述Java文件,结果如下:
D:\聚划算\技术部\编程练习\Velocity\testVelocity>java HelloWor
Hello Eiffel Qiu! Welcome to http://www.j2medev.com world!
后记:
1、调试过程中,刚开始在classpath中只设置了velocity-1.6.3.jar,调试后发现缺class,缺的class正好在velocity-1.6.3-dep.jar中,引入后问题解决
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏