当前位置: 网学 > 编程文档 > 其他类别 > 正文

模板引擎 Velocity

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/08/11

网学网为广大网友收集整理了,模板引擎 Velocity,希望对大家有所帮助!

Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点,也就是说,页面设计人员可以只 关注页面的显示效果,而由java程序开发人员关注业务逻辑编码。Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提 供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。 Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当 作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。Velocity也可以为Turbine web开发架构提供模板服务(template service)。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。

实现Velocity模板技术有以下步骤

一、下载velocity.ziphttp://velocity.apache.org/download.cgi二、创建普通java工程,导入velocity-1.7.jar、commons-lang-2.4.jar、commons-collections-3.2.1.jar三、实现velocity代码流程,创建VelocityTest.java,引入单元测试1、初始化@BeforeClasspublic static void setUpBeforeClass() {// Velocity.init(“src/velocity.properties”); // 读配置文件方式// 程序方式编写属性Properties properties = new Properties();properties.put(“runtime.log”, “G:\workspace\java\Velocity\log”);properties.put(“file.resource.loader.path”, “G:\workspace\java\Velocity\vm”);properties.put(“input.encoding”, “UTF-8″);properties.put(“output.encoding”, “UTF-8″);Velocity.init(properties);}@Testpublic void hello() throws Exception {// 1、初始化// Velocity.init(“src/velocity.properties”); // velocity.properties文件内容见代码流程后面// 2、创建一个上下文对象VelocityContext context = new VelocityContext();// 3、往上下文对象中加入对象context.put(“hello”, “你好,欢迎来到广州”);// 4、选择一个模板Template template = Velocity.getTemplate(“hello.vm”); // hello.vm文件内容见代码流程后面// 5、执行合并操作StringWriter writer = new StringWriter();template.merge(context, writer);writer.close();System.out.println(writer.toString());}velocity.propertiesruntime.log=G:\workspace\java\Velocity\logfile.resource.loader.path=G:\workspace\java\Velocity\vminput.encoding=UTF-8output.encoding=UTF-8#directive.foreach.counter.name=velocityCountdirective.foreach.counter.name=index#directive.foreach.counter.initial.value=1directive.foreach.counter.initial.value=0hello.vm${hello}// 利用模板技术生成文件@Testpublic void outFile() throws Exception {// Velocity.init(“src/velocity.properties”);Product product = new Product(1, “篮球”, null);VelocityContext context = new VelocityContext();context.put(“product”, product);Template template = Velocity.getTemplate(“productTemplate.html”);// 创建输出文件File saveFile = new File(“html/product.html”);if (!saveFile.getParentFile().exists()) {saveFile.getParentFile().mkdirs();}FileOutputStream outStream = new FileOutputStream(saveFile);OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, “UTF-8″);BufferedWriter writer = new BufferedWriter(outStreamWriter);template.merge(context, writer);writer.flush();outStream.close();outStreamWriter.close();writer.close();}

  • 上一篇资讯: build.xml详解
  • 网学推荐

    免费论文

    原创论文

    浏览:
    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
    湘ICP备09003080号