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,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· .NET 8.0 + Linux 香橙派,实现高效的 IoT 数据采集与控制解决方案
· .NET中 泛型 + 依赖注入 的实现与应用