http://www.cnblogs.com/amboyna/archive/2008/06/19/1225105.html
现在发现了Apache Velocity,好东西呀,可以应用在很多领域,目前只用到他在Struts中做页面显示,以免同一个url负载过重(如:shownews.do,可能同时N个人看新闻,这个新闻详细内容显示的URL负载就过重了)。好了,大概原理如下图:
模板(h.vm)只是提供一些静态的字符,中间加杂着一些类如$title的“通配符”,这是要在HelloWorld.java中替换的。下面是h.vm的代码:
Hello $name! Welcome to $site world!
HelloWorld.java就是处理程序了,其中注释写的很清楚了,下面是HelloWorld.java的代码:
import java.io.StringWriter;
import org.apache.velocity.app.Velocity;
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 {
// 初始化Velocity模板引擎
VelocityEngine ve = new VelocityEngine();
ve.init("D:\\working\\Velocity\\aaaa\\bin\\a.properties");
// Velocity获取模板文件,得到模板引用
Template t = ve.getTemplate("h.vm");
// 初始化环境,并将数据放入环境
VelocityContext context = new VelocityContext();
context.put("name", "zz");
context.put("site", "http://amboyna.cnblogs.com");
// 将环境变量和输出部分结合
StringWriter writer = new StringWriter();
t.merge(context, writer);
System.out.println(writer.toString());
}
}
在这里,配置文件主要的作用就是提供路径,不多说了,下面是a.properties的代码:
runtime.log = velocity_example.log
input.encoding=gbk
output.encoding=gbk
file.resource.loader.path=D:\\working\\Velocity\\aaaa\\bin
分享到:
相关推荐
a book of Apache Velocity
NULL 博文链接:https://trinea.iteye.com/blog/1157849
Apache Velocity - Velocity User Guide PDF格式文档,便于离线阅读。
Apache Velocity - VTL Reference just for convenience, it's from official site documentation
掌握Velocity,唯一一本Velocity书,翻译的整理本
Apache Velocity项目Apache Velocity网站介绍当您访问 (我们的主页)时,将获得Apache Velocity网站。 它是所有Apache Velocity子项目的信封站点。 简而言之:仅当您感兴趣时,此Git存储库才对您很有趣a)想要在...
很全的讲解velocity的书
web开发的,Velocity,简单环境实例,已通过运行,
Apache Velocity - Developer's Guide PDF格式文档,便于离线阅读。
基本理念通过尝试将模板与格式化文本进行匹配来填充 Apache Velocity 上下文(又名字符串 -> 对象映射)。调用例子: import stillness.Stillness;import stillness.ScrapeException;import org.apache.velocity....
Apache Velocity是用Java编写的通用模板引擎。 有关Velocity的更多信息,请参阅HTML文档。 这是顶级目录的描述: velocity-engine-core/ The Velocity Engine core modulevelocity-engine-examples/ Several simple...
apache-velocity提供了强大了模板引擎功能,为我们动态生成代码及代码片段提供了很好地支持,本资源包含了velocity使用文档及相应的jar包
简单的验证工具,用于检测Apache Velocity模板中的错误。 它遍历目录并验证每个.vm文件。 错误会显示文件名,错误和可能的解决方案。 用法 java -jar velocityvalidator.jar path_to_templates [-rules=path_to_...
org.apache.velocity_1.3.1 jquery 插件, apache 提供的一个强大的插件, 大大的方便了程序员的开发.
velocity生成静态页面实例,自己写的,运行就可以看到效果
NULL 博文链接:https://javaocean.iteye.com/blog/256655