zoukankan      html  css  js  c++  java
  • 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用

    原文地址http://www.itzhai.com/the-introduction-of-the-velocity-template-engine-template-language-syntax-and-basic-use.html

    类似于PHP中的Smarty,Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。从而实现界面和Java代码的分离,使得界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点。

    另外,Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。Velocity也可以为Turbine web开发架构提供模板服务(template service)。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。

    编写Velocity版的Hello World

    获取Velocity相关JAR文件:

    从http://velocity.apache.org/网站上下载最新的Velocity,这里我们下载了velocity-1.7.zip

    相关Jar包添加到项目中:

    解压velocity-1.7.zip,发下其根目录下有两个JAR文件:

    velocity-1.7.jar velocity-1.7-dep.jar

    其中velocity-1.7-dep.jar包含了:

    velocity-1.7.jar commons-collections-3.2.1.jar commons-lang-2.4.jar oro-2.0.8.jar(这些JAR文件位于解压目录的lib目录下)

    在JAR包不冲突的情况下可以直接使用velocity-1.7-dep.jar

    载类路径下添加velocity.properties文件:

    该文件一般包含如下配置:

    runtime.log = F:projectMusicalInstrumentsStorevelocity_example.log
    file.resource.loader.path = F:projectMusicalInstrumentsStorevm
    input.encoding = UTF-8
    output.encoding = UTF-8

    runtime.log指定日志文件存放位置

    file.resource.loader.path指定模板的加载位置
    input.encoding指定输入编码
    output.encoding指定输出编码

    在Java文件中初始化Velocity模板引擎并设置Velocity上下文的一些变量然后把生成的模板输出到StringWriter:
    //初始化模板引擎
    Velocity.init("src/velocity.properties");
    //获取VelocityContext
    VelocityContext context = new VelocityContext();
    //为Context设置变量
    context.put("title", "HelloWorld");
    context.put("author", "arthinking");
    //获取模板文件
    Template template = Velocity.getTemplate("helloworld.vm");
    StringWriter sw = new StringWriter();
    //使用模板文件的merge函数合并模板和context提供的变量,输出到StringWriter中
    template.merge(context, sw);
    sw.flush();
    System.out.println(sw.toString());
    编写helloworld.vm模板文件(保存在file.resource.loader.path设置的目录下):
    ${who}
    ${content}

    运行Java文件,使用Velocity生成的信息就打印出来了。

    注:如果who为空时,${who}会原样输出,为了使之不输出,可以在$后加个!:$!{who}

    Velocity模板语言基本语法

    访问对象属性:

    和使用EL表达式差不多,直接使用”.”导航。
    如访问object对象的id属性:${object.id }

    遍历List集合:
    #foreach($element in $list)
        #element
    #end

    使用判断语句:

    #if($condition)
        true
    #else
        false
    #end

    获取迭代索引值:

    默认使用变量名:$velocityCount
    也可以自定义此变量名,在velocity.properties中设置:

    directive.foreach.counter.name=index

    设置索引起始位置为0:

    directive.foreach.counter.initial.value=0

    遍历Map变量:

    #foreach($key in $map.keySet())
        $key : $map.get($key)
    #end
    在模板中进行赋值:
    #set(#a=”Hello World!”)
    $a
    
    #set($array1=[1..10])
    #foreach($entry in $array1)
        #entry
    #end
    使用Velocity模板引擎生成文件:
    //初始化模板引擎
    Velocity.init("src/velocity.properties");
    //获取VelocityContext
    VelocityContext context = new VelocityContext();
    //为Context设置变量
    context.put("content", "HelloWorld");
    context.put("who", "arthinking");
    //获取模板文件
    Template template = Velocity.getTemplate("helloworld.vm");
    //创建输出文件
    File output = new File("D:/Velocity/1.html");
    if(!output.getParentFile().exists())
        output.getParentFile().mkdir();
    //创建输出流
    FileOutputStream outputStream = new FileOutputStream(output);
    OutputStreamWriter writer = new OutputStreamWriter(outputStream);
    BufferedWriter bufferedWriter = new BufferedWriter(writer);
    template.merge(context, bufferedWriter);
    
    bufferedWriter.flush();
    outputStream.close();
    bufferedWriter.close();
  • 相关阅读:
    Android uiautomator实例使用
    android自动化之MonkeyRunner测试环境配置(一)
    移动平台自动化测试从零开始--MonkeyRunner工具使用_正版电子书在线阅读_百度阅读
    android sdk工具之MonkeyRunner (实例)_小兵兵同学_新浪博客
    数据库~Mysql里的Explain说明
    数据库~Mysql派生表注意的几点~关于百万数据的慢查询问题
    ELK系列~Fluentd对大日志的处理过程~16K
    LindDotNetCore~ISoftDelete软删除接口
    DotNetCore跨平台~认识环境和环境变量
    Oracle V$SQLAREA
  • 原文地址:https://www.cnblogs.com/ryuasuka/p/3507202.html
Copyright © 2011-2022 走看看