使用maven引入jar

<dependency><groupId>org.apache.velocity</groupId><artifactId>velocity</artifactId><version>1.7</version>
</dependency>
<dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-tools</artifactId><version>2.0</version><exclusions><exclusion><groupId>org.apache.struts</groupId><artifactId>struts-tiles</artifactId></exclusion><exclusion><groupId>org.apache.struts</groupId><artifactId>struts-taglib</artifactId></exclusion><exclusion><groupId>org.apache.struts</groupId><artifactId>struts-core</artifactId></exclusion><exclusion><groupId>sslext</groupId><artifactId>sslext</artifactId></exclusion><exclusion><groupId>oro</groupId><artifactId>oro</artifactId></exclusion></exclusions>
</dependency><dependency><groupId>commons-configuration</groupId><artifactId>commons-configuration</artifactId><version>1.10</version></dependency><dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.3</version></dependency>

创建TemplateUtil工具类

package com.os.core.util.web;import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;public class TemplateUtil {public static void main(String[] args) {test();}/*** 测试方法*/public static void test() {try {String vmContent = "${dataMap.value1}中文<br/>${dataMap.value2}";String vmFileUrl = "";//CommonConstants.templateFileDir;//生成模板文件String fileName = createTemplateFile(vmContent, vmFileUrl);//模板上下文Map<String, Object> dataMap = new HashMap<String, Object>();dataMap.put("value1", "ssssssssssssssssssssss");dataMap.put("value2", "中文");VelocityContext context = new VelocityContext();context.put("dataMap", dataMap);//获取模板生成器VelocityEngine engine = initEngine("");//CommonConstants.templateFileDir//html文件生成全路径File file = new File("", "UTF-8");//CommonConstants.htmlFileDir+"/index.html"boolean createSuccess = createHtmlFile(engine, fileName, file, context);if (createSuccess) {System.out.println("文件生成完成");} else {System.out.println("文件生成失败");}} catch (Exception e) {e.printStackTrace();}}/*** 生成静态的HTML文件** @param engine       模板生成器* @param tempFileName 模板文件* @param file         HTML生成的全路径* @param context      模板上下文* @return true生成成功 false生成失败* @throws Exception*/public static boolean createHtmlFile(VelocityEngine engine, String tempFileName, File file, VelocityContext context) {try {if (file.getParentFile().exists() == false) {file.getParentFile().mkdirs();}Template temp = engine.getTemplate(tempFileName, "UTF-8");FileOutputStream tempFos = new FileOutputStream(file);BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(tempFos, "UTF-8"));temp.setEncoding("UTF-8");temp.merge(context, writer);writer.close();tempFos.close();return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 初始化模板生成器** @return VelocityEngine* @throws Exception*/public static VelocityEngine initEngine(String genDir) throws Exception {VelocityEngine engine = new VelocityEngine();Properties properties = new Properties();properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, genDir);properties.setProperty(VelocityEngine.INPUT_ENCODING, "UTF-8");properties.setProperty(VelocityEngine.OUTPUT_ENCODING, "UTF-8");engine.init(properties);return engine;}/**** 创建模板文件** @param fileContext  模板内容* @param createDirUrl 模板保存目录*/public static String createTemplateFile(String fileContext, String createDirUrl) {BufferedWriter bw = null;try {String fileDirUrl = createDirUrl + ".vm";File file = new File(fileDirUrl);if (file.getParentFile().exists() == false) {file.getParentFile().mkdirs();}bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));bw.write(fileContext);return fileDirUrl;} catch (Exception e) {System.out.println("生成模板文件出错");e.printStackTrace();return null;} finally {if (bw != null) {try {bw.close();} catch (IOException e) {e.printStackTrace();}}}}
}

使用velocity相关推荐

  1. 卡尔曼滤波— Constant Velocity Model

    假设你开车进入隧道,GPS信号丢失,现在我们要确定汽车在隧道内的位置.汽车的绝对速度可以通过车轮转速计算得到,汽车朝向可以通过yaw rate sensor(A yaw-rate sensor is ...

  2. aa bb ccc java,TinyTemplate(Velocity Plus版)即将火热推出~~~

    原本是没有本身写一个模板引擎的计划的,由于按个人理解,一直认识这种"语言"级的引擎,难度是很是大的.总感受本身的水平不够,所以不敢有这个念头.直到大量使用Velocty的时候,碰到 ...

  3. Velocity 入门(一)

    Velocity是一种Java模版引擎技术,该项目由Apache提出.因为非常好用,和工作中有啥用,所以我在在理简单的入门一下. 网上找了很多教程,写的不是很明白,要么就是全部拷贝下来时候运行不起来. ...

  4. spring mvc velocity 配置备忘

    2019独角兽企业重金招聘Python工程师标准>>> Spring里面最重要的概念是IOC和AOP,还有两项很重要的模块是事务和MVC,对于IOC和AOP,我们要深究其源码实现,对 ...

  5. Velocity判断空的方法

    Velocity中没有null,那么怎么判断null呢 1.在velocity中,非null被认为是真的,所以,可以如下用: #if($!变量名)// 变量不为空的代码 #else// 变量为空的代码 ...

  6. Velocity文档(3)

    2019独角兽企业重金招聘Python工程师标准>>>     velocity.properties 的一些配置项 velocimcro.library属性:指定自己的模板库,多个 ...

  7. velocity自定义标签和指令

    velocity本身支持自定义标签和指令的扩展, 在 Velocity 模板语言的语法中,以美元符 $ 开头的为变量的声明或者引用,而以井号 # 开头的语句则为 Velocity 的指令(Direct ...

  8. java中velocity定义宏标签_velocity自定义标签和指令(转:zwj)

    velocity本身支持自定义标签和指令的扩展,我们看看扩展指令的步骤及searchweb2的应用场景, 1.使用方法 在 Velocity 模板语言的语法中,以美元符 $ 开头的为变量的声明或者引用 ...

  9. Velocity笔记--使用Velocity获取动态Web项目名的问题

    以前使用jsp开发的时候,可以通过request很轻松的获取到根项目名,现在换到使用velocity渲染视图,因为已经不依赖servlet,request等一些类的环境,而Web项目的根项目名又不是写 ...

  10. freemarker中运算符_如何在Web应用系统表示层开发中应用Velocity模板技术

    软件项目实训及课程设计指导--如何在Web应用系统表示层开发实现中应用Velocity模板技术 1.分离Web表示层的数据处理和展现逻辑的常见的应用技术 分离Web表示层的数据处理和展现逻辑是目前企业 ...

最新文章

  1. DPDK — CLI 指令行模块
  2. 2018.3.31 设计模式之生成器模式详解及例子(对象创建型模式)
  3. 微软向马斯克的人工智能项目OpenAI投资10亿美元
  4. java的int、char、long、float、double对byte的转换,在通信的时候会用到
  5. C++ 重载数学运算符
  6. MATLAB基础教程(5)——斐波那契数列
  7. Selenium学习(2) 元素定位
  8. VS2013模块对于SAFESEH映像是不安全的解决方法
  9. pptx库ppt演示 python_Python自动化操作PPT看这一篇就够了
  10. 浅谈嵌入式软件的未来发展
  11. 【转】Golang 新手可能会踩的 50 个坑
  12. Cesium:加载本地高程/地形数据
  13. 如果同时需要两张表,但其中一个表中没有另一个表中的字段,该如何正确使用
  14. 安装SQL 2016的时候 Microsoft R Open 和 Microsoft R Server 安装文件的位置
  15. 以下计算机网络新技术是什么,计算机网络新技术概述
  16. 启动Activity时显示空白界面的问题
  17. jdom生成与解析xml详解
  18. android模拟器 vt,逍遥安卓模拟器怎么开启VT模式 VT虚拟化设置方法
  19. 卧槽!微信头像可以带圣诞帽啦!
  20. c#语言中怎么样把文本转换成数字,如何将字符串转换为数字 - C# 编程指南 | Microsoft Docs...

热门文章

  1. 20.if条件语句.rs
  2. Kafka设计解析(四):Kafka Consumer解析
  3. cocos2d-x游戏实例(4)-地图碰撞
  4. 一些学习cocos2d的网站
  5. Anbox 实现分析 3:会话管理器与容器管理器的通信
  6. 计算机专业有什么血泪建议吗?
  7. Redis功能强大,那也顶不住被滥用啊!
  8. OS- -调度(二)
  9. 音视频技术开发周刊 58期
  10. Netflix如何节省92%视频编码成本?