Atitit velocity 模板引擎使用法

目录

1.1. 1.4 Context 1

1.1.1. 1.4.1 Context 基本概念 1

1.2.   .3不同模式下使用velocity 1

1.2.1. 1.3.1 单例模式使用velocity 1

1.2.2. 1.3.2 多实例使用velocity 2

2. 字符串模板而不是文件模式 2

2.1. Code 3

  1. 1.4 Context

    1. 1.4.1 Context 基本概念

Context是作为数据容器的,实现是基于HashMap的,模版中用到的变量都必须放到context中,类似于spring中的ModelAndView。没有特殊需求的话可以使用官方推荐的VelocityContext,不支持jdk对象序列化。
VelocityContext构造器默认实现HashMap:

    1. .3不同模式下使用velocity

  • 单例模式使用: org.apache.velocity.app.Velocity
  • 多实例模式使用: org.apache.velocity.app.VelocityEngine
    1. 1.3.1 单例模式使用velocity

//初始化Velocity

Velocity.init();//创建context

VelocityContext context = new VelocityContext();//添加数据

context.put( "name", new String("Velocity") );//获取模版

Template template = Velocity.getTemplate("templates/helloworld.vm");//Merge 模版和context

StringWriter writer = new StringWriter();template.merge(context, writer);

System.out.println(writer.toString());

  1. 1.3.2 多实例使用velocity

//每次创建VelocityEngine对象

VelocityEngine ve = new VelocityEngine();

ve.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);

ve.init();//创建context

VelocityContext context = new VelocityContext();//添加数据

context.put( "name", new String("Velocity") );//获取模版

Template template = Velocity.getTemplate("templates/helloworld.vm");//Merge 模版和context

StringWriter writer = new StringWriter();template.merge(context, writer);

System.out.println(writer.toString());

  1. 字符串模板而不是文件模式

public static String getTmpltCalcRzt(String sqlTmplt, VelocityContext context) {
   // 初始化并取得Velocity引擎
         VelocityEngine ve = new VelocityEngine();
         ve.init();

// 取得velocity的模版内容, 模板内容来自字符传

         // 输出流
         StringWriter writer = new StringWriter();

// 转换输出   con,writer,loigtag,tmplt

         ve.evaluate(context, writer, "", sqlTmplt); // 关键方法

         String rzt = writer.toString();
         return rzt;
}

  1. Code

package org.chwin.firefighting.apiserver.dsl;

import java.io.StringWriter;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class velocityUtil {

public static void main(String[] args) {
      String t="";
      String u_col=" '$uname','$usertype' ";
//创建context
      VelocityContext context = new VelocityContext();
//添加数据
      context.put( "uname", new String("艾龙") );
      context.put( "usertype", new String("维保公司") );
      System.out.println( getTmpltCalcRzt(u_col,context) );
   }

//@depr
   public static String getTmpltCalcRztV2(String sqlTmplt, VelocityContext context) {
      // 初始化并取得Velocity引擎
      VelocityEngine ve = new VelocityEngine();
      ve.init();

// 取得velocity的模版内容, 模板内容来自字符传

      // 输出流
      StringWriter writer = new StringWriter();

// 转换输出   con,writer,loigtag,tmplt

      ve.evaluate(context, writer, "", sqlTmplt); // 关键方法

      String rzt = writer.toString();
      return rzt;
   }

public static String getTmpltCalcRzt(String sqlTmplt, VelocityContext context) {
      // 初始化并取得Velocity引擎
            VelocityEngine ve = new VelocityEngine();
            ve.init();

// 取得velocity的模版内容, 模板内容来自字符传

            // 输出流
            StringWriter writer = new StringWriter();

// 转换输出   con,writer,loigtag,tmplt

            ve.evaluate(context, writer, "", sqlTmplt); // 关键方法

            String rzt = writer.toString();
            return rzt;
   }

}

Atitit velocity 模板引擎使用法 目录 1.1. 1.4 Context 1 1.1.1. 1.4.1 Context 基本概念 1 1.2. .3不同模式下使用velocity 1相关推荐

  1. velocity模板引擎帮助

    Velocity用户手册是帮助页面设计者和内容提供者认识Velocity和其简单而功能强大的脚本语言――Velocity模板语言(VTL).在手册上的许多例子,都是用Velocity插入动态的内容到网 ...

  2. velocity模板引擎-vm语法整理

    velocity模板引擎-vm语法整理 模板引擎我目前接触过两次, 第一次是在前后端还未分离时,考虑到部分页面数据比较固定,一定时间内不会变动,这样每次从后台加载数据渲染页面明显会消耗性能以及资源浪费 ...

  3. spring boot模板引擎thymleaf用法详解

    spring boot模板引擎thymleaf用法详解 Spring-boot支持FreeMarker.Thymeleaf.jsp.veocity 但是对freemarker和thymeleaf的支持 ...

  4. pug模板引擎基本用法

    一.pug模板引擎基本用法 Pug是jade的新版,jade由于商标已被注册,所以更名为pug. 缩进标注:Pug模板和大多数模板不同,它有自己的语法特点,采用缩进的方式进行标注,使用这种方式,使得代 ...

  5. 工具集核心教程 | 第五篇: 利用Velocity模板引擎生成模板代码

    前言 不知道大家有没有这样的感觉,在平时开发中,经常有很多dao.service类中存着很多重复的代码,Velocity提供了模板生成工具,今天我教大家怎么和这些大量的重复代码说再见. 参考项目:ht ...

  6. html转义成velocity,改造Velocity模板引擎让$[!]{}输出默认进行html转义,并增加$#{}语法支持不转义输出...

    一直以来在项目中使用Apache Velocity模板引擎作为视图层输出,为了解决XSS漏洞,需要对输出到页面的内容进行HTML转义,我一般采用2种方式实现: 使用过滤器 Filter,在其中进行 H ...

  7. 使用 Velocity 模板引擎快速生成代码

    Velocity 模板引擎介绍 在现今的软件开发过程中,软件开发人员将更多的精力投入在了重复的相似劳动中.特别是在如今特别流行的 MVC 架构模式中,软件各个层次的功能更加独立,同时代码的相似度也更加 ...

  8. Velocity模板引擎实现通用导出

    Velocity模板引擎实现通用导出 基础配置 在数据库中配置导出sql模板,导出文件名,sheet名,字段对应中文名等.表结构如下 CREATE TABLE `finance_download_co ...

  9. Velocity模板引擎

    velocity简介 velocity介绍 Velocity是一个基于Java的模板引擎,可以通过特定的语法获取在java对象的数据,填充到模板中,从而实现界面和java代码的分离 应用场景 Web应 ...

  10. Velocity模板引擎实战:动态生成HTML、Word、Excel等报表

    上篇文章介绍了Velocity的基本用法,文章链接:Java使用 VelocityEngine模板引擎快速生成HTML等各种代码 本篇文章主要针对Velocity的实际应用 先来一个工具类 packa ...

最新文章

  1. Ubuntu13.04下使用dnw
  2. HashSet与HashMap源代码深度剖析
  3. 数据库经典文章!(必备)
  4. 车纷享:基于阿里云HBase构建车联网平台实践
  5. 一程序员被判 9 个月:因薪酬等问题离职,rm -f * 删库,瘫痪 6 个小时
  6. 移动站原生的select实现省市区联动选择
  7. 机器学习ai选股_自带AI机器学习的MEMS了解一下
  8. 企业微信开放客户朋友圈功能 无需审核即可使用
  9. [蓝点zigBee] CC2530 实用教程总览
  10. Hadoop2 自己动手编译Hadoop的eclipse插件
  11. 蓝宝石rx470d原版bios_小白福利教程:关于显卡BIOS的一些信息的讲解
  12. 主动微波遥感和被动微波遥感
  13. Oracle中表pagesize,Oracle使用pagesize命令
  14. java毕业生设计二手物品交易系统计算机源码+系统+mysql+调试部署+lw
  15. 从随机生成九宫格至随机生成数独游戏再至用回溯法实现数独的解
  16. LWN:32位Linux的未来!
  17. 3DMax如何将一个整的模型将里面组件都分离出来
  18. HDU 1867(kmp应用)
  19. 以 Broadcom Corporation BCM4311、BCM4312、BCM4313、BCM4321 或 BCM4322 为基础的无线网络卡...
  20. JNI系列(一)JNI简介

热门文章

  1. cnblog中添加数学公式支持
  2. grafana 部署
  3. 综合布线系统入门及应用(一)
  4. 设计模式-第四篇之代理模式
  5. [No000014B]Office-PPT设置默认打开视图
  6. JS Range使用整理
  7. 项目部署到自己的IIS上
  8. jgit - java实现git操作
  9. View绘制详解(四),谝一谝layout过程
  10. UILabel根据内容自动调整高度