zoukankan      html  css  js  c++  java
  • Velocity模版使用

    <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.7</version>
    </dependency>

     http://mvnrepository.com/artifact/org.apache.velocity

    <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-tools -->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-tools</artifactId>
        <version>2.0</version>
    </dependency>

    引入的jar包:

    1 import org.apache.velocity.Template;
    2 import org.apache.velocity.VelocityContext;
    3 import org.apache.velocity.app.Velocity;
    4 import org.apache.velocity.app.VelocityEngine;
    5 import org.apache.velocity.tools.generic.DateTool;
    6 import org.apache.velocity.tools.generic.NumberTool;

    jar包可以去官网下载 

    http://velocity.apache.org/engine/devel/

    样例代码:

     1 /**
     2      * 生成vm
     3      * @param vmpackage vm所在的包名  
     4      * @param vmname
     5      * @param listname
     6      * @param objects
     7      * @return
     8      * @throws Exception
     9      */
    10     public static String generateVm(String vmpackage, String vmname, String listname, List<? extends Object> objects) throws Exception{
    11         if(objects.size()>0){
    12             Properties p = new Properties(); 
    13             String classpath = VelocityUtils.class.getResource("/").getPath();//取得src的路径
    14             p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, classpath + vmpackage);
    15             //设置velocity的编码 
    16             p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8"); 
    17             p.setProperty(Velocity.INPUT_ENCODING, "UTF-8"); 
    18             p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8"); 
    19             //初始化并取得Velocity引擎
    20             VelocityEngine ve = new VelocityEngine();
    21             ve.init(p);
    22             //取得velocity的模版
    23             Template t = null;
    24             t = ve.getTemplate(vmname);
    25             //取得velocity的上下文context 
    26             VelocityContext context = new VelocityContext();
    27             context.put("number", new NumberTool());
    28             context.put("date", new DateTool());
    29             context.put(listname, objects);
    30             //输出流 
    31             StringWriter writer = new StringWriter();
    32             //转换输出 
    33             t.merge(context, writer); 
    34             return writer.toString();
    35         }else{
    36             return null;
    37         }
    38     }

    上面函数调用:

    1 String vmpagckage = "com/cpinfo/his/template/emr/";
    2             String vmname = "partogram_detail_infos.vm";
    3             String vm = VelocityUtils.generateGridVm(vmpagckage, vmname, "bs", ems);
    4             
    5             response.setContentType("text/xml;charset=utf-8");
    6             PrintWriter pw = response.getWriter();
    7             pw.print(vm);
    8             pw.flush();
    9             pw.close();

    模版文件的内容例子:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <rows>
     3     #foreach($b in $bs)
     4     <row id="$b.detailinfoid">
     5         <cell title="$!date.format('yyyy-MM-dd HH:mm',$b.time)">$!date.format('HH:mm',$b.time)</cell>
     6         <cell>$!number.format($b.gongkoukaida)</cell>
     7         <cell>$!number.format($b.xianluxiajiang)</cell>
     8         #if($b.taiermianchu==1)
     9         <cell>是</cell>
    10         #else
    11         <cell>否</cell>
    12         #end
    13         <cell>$!b.taixinyin</cell>
    14         <cell>$!b.mailv</cell>
    15         #if($b.xueyagao || $b.xueyadi)
    16         <cell>$!b.xueyagao/$!b.xueyadi</cell>
    17         #else
    18         <cell></cell>
    19         #end
    20         <cell>$!b.teshuzhiliao</cell>
    21         <cell>$!b.qianming</cell>
    22         <cell><![CDATA[<img src="img/emr/del_package.png" alt="删除" style="cursor:pointer;" onclick="javascript:delDetailInfo('$!b.detailinfoid')"/>]]></cell>
    23     </row>
    24     #end
    25 </rows>

    模版文件的语法 等可以去官网看文档。

  • 相关阅读:
    前端性能优化(2.1 代码分离——入口起点 entry point)
    前端性能优化(prefetch, preload, dns-prefetch, defer, async)
    Golang-函数
    Golang复合类型-map
    Golang复合类型-切片
    Golang复合类型-数组
    Golang基础语法-goto语句
    Golang基础语法-跳出循环
    vueRouter学习笔记
    通过ES6实现个模板操作
  • 原文地址:https://www.cnblogs.com/lishupeng/p/5494269.html
Copyright © 2011-2022 走看看