• Velocity魔法堂系列一:入门示例(转)


    Velocity魔法堂系列一:入门示例 

    一、前言                          

      Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本处理能力。而且Velocity被移植到不同的平台上,如.Net的NVelocity和js的Velocity.js,虽然各平台在使用和实现上略有差别,但大部分语法和引擎核心的实现是一致的,因此学习成本降低不少哦。

      最好的学习资源——官网:http://velocity.apache.org/

      本系列打算采用如下结构对Velocity进行较为全面的学习,若有不妥或欠缺望大家提出,谢谢。

      1. 入门示例

      2. VTL语法详解

      3. 模板与宿主环境通信

      4. 基础配置项

      5. 深入模板引擎及调优配置

    二、入门示例                        

      示例结果是生成如下的html表单:

    复制代码
    <form action="./submit">
    <div>
      <label for="title">标题:</label>
      <input type="text" id="title" name="title"/>
    </div>
    <div>
      <label for="brief">摘要:</label>
      <input type="text" id="brief" name="brief"/>
    </div>
    <div>
      <label for="sex">性别:</label>
      <select id="sex" name="sex">
        <option value="0">男</option>
        <option value="1">女</option>
      </select>
    </div>
    <div>
      <label for="job">职业:</label>
      <select id="job" name="job">
        <option value="0">Java工程师</option>
        <option value="1">Net工程师</option>
      </select>
    </div>
    </form>
    复制代码

       引入依赖项——velocity-1.7-dep.jar

       模板文件frm.vm

    复制代码
    ##表单模板
    ##@author fsjohnhuang
    ##@version 1.0
    ## 引入外部模板文件
    #parse('macro.vm')
    ## 主逻辑
    <form action="$action">
    #foreach($input in $inputs)
    #input($input.title $input.id)
    #end
    #foreach($select in $selects)
    #select($select.title $select.id $select.items)
    #end
    </form>
    复制代码

       模板文件macro.vm

    复制代码
    ## 生成input表单元素区域的宏
    #macro(input $title $id)
    <div>
      <label for="$id">$title</label>
      <input type="text" id="$id" name="$id"/>
    </div>
    #end
    ## 生成select表单元素区域的宏
    #macro(select $title $id $items)
    <div>
      <label for="$id">$title</label>
      <select id="$id" name="$id">
    ## VTL指令紧贴左侧才能确保结果的排版正常(不会有多余空格)
    #foreach($key in $items.keySet())
        <option value="$key">$items.get($key)</option>
    #end
      </select>
    </div>
    #end
    复制代码

       Java代码:

    复制代码
    public static void main(String[] args) {
            // 初始化模板引擎
            Properties props = new Properties();
            props.put("file.resource.loader.path", ".\vm");
            VelocityEngine ve = new VelocityEngine(props);
            // 配置引擎上下文对象
            VelocityContext ctx = new VelocityContext();
            ctx.put("action", "./submit");
            ArrayList<HashMap<String, String>> inputs = new ArrayList<HashMap<String,String>>();
            HashMap<String, String> input1 = new HashMap<String, String>();
            input1.put("id", "title");
            input1.put("title", "标题:");
            inputs.add(input1);
            HashMap<String, String> input2 = new HashMap<String, String>();
            input2.put("id", "brief");
            input2.put("title", "摘要:");
            inputs.add(input2);
            ctx.put("inputs", inputs);
            ArrayList<HashMap<String, Object>> selects = new ArrayList<HashMap<String,Object>>();
            HashMap<String, Object> select1 = new HashMap<String, Object>();
            selects.add(select1);
            select1.put("id", "sex");
            select1.put("title", "性别:");
            HashMap<Integer, String> kv1 = new HashMap<Integer, String>();
            kv1.put(0, "男");
            kv1.put(1, "女");
            select1.put("items", kv1);
            HashMap<String, Object> select2 = new HashMap<String, Object>();
            selects.add(select2);
            select2.put("id", "job");
            select2.put("title", "职业:");
            HashMap<Integer, String> kv2 = new HashMap<Integer, String>();
            kv2.put(0, "Java工程师");
            kv2.put(1, "Net工程师");
            select2.put("items", kv2);
            ctx.put("selects", selects);
            // 加载模板文件
            Template t = ve.getTemplate("test.vm");
            StringWriter sw = new StringWriter();
            // 渲染模板
            t.merge(ctx, sw);
            System.out.print(sw.toString());
        }
    复制代码

     Velocity模板引擎使用时的关注点分别为以外部文件形式存在的Velocity模板Java代码调用

     Velocity模板由VTL(Velocity Template Language)引擎上下文对象构成;Java代码调用部分则负责初始Velocity引擎构建引擎上下文对象加载Velocity模板启动模版渲染。而Velocity模板与Java代码调用部分通信的纽带就是引擎上下文对象了。

    三、总结                              

     现在我们对Velocity引擎应该有个大概的了解,后续内容将对上述内容逐一深入。

     尊重原创,转载请注明来自:http://www.cnblogs.com/fsjohnhuang/p/4112328.html  ^_^肥仔John

     吐槽:倘若对文本的排版有强烈的要求,那么Velocity就不是最佳选择了。

     如上述示例,若想改成如下格式就要重新设计模板形式了:

    复制代码
    <form action="./submit">
      <div>
        .................
      </div>
      <div>
        .................
      </div>
      <div>
        .................
      </div>
      <div>
        .................
      </div>
    </form>
    复制代码

    如果您觉得本文的内容有趣就扫一下吧!捐赠互勉!

  • 相关阅读:
    ReentrantReadWriteLock锁例子
    线程安全的atomic wrapper classes例子
    统一处理jquery ajax请求过程中的异常错误信息的机制
    理解RESTful架构
    适配器模式的两种实现方式
    第一部分:开发前的准备-第一章 什么是Andorid
    cocos2d-x开发记录:二,基本概念(骨骼动画)
    cocos2d-x开发记录:二,基本概念(粒子系统,Scheduler和定时器)
    cocos2d-x开发记录:二,基本概念(导演,场景,层和精灵,场景切换,效果)
    cocos2d-x开发记录:二,基本概念(动作,动画,坐标系统)
  • 原文地址:https://www.cnblogs.com/iceyhu/p/4618918.html
Copyright © 2020-2023  润新知