Velocity根据模版生成静态html
新公司的一个CMS项目要用到,这里记录下
一、项目文件图

二、springmvc-servlet.xml 添加
<!-- 定义环境变量文件 -->
<bean id="propertyHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath*:/*.properties</value>
</list>
</property>
</bean>
三、html_template.vm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>${list.title}</title>
</head>
<body>
<h2>${list.title}</h2>
<table border="1" style="margin-left: 100px" >
<tr>
<th class="jobs-time">序号</th>
<th class="jobs-title">名称</th>
<th class="jobs-title">手机</th>
<th class="jobs-title">邮箱</th>
</tr>
#if($!list)
<tr>
<td>${list.userId}</td>
<td>${list.userName}</td>
<td>${list.mobile}</td>
<td>${list.email}</td>
</tr>
#end
</table>
</body>
</html>
四、template.properties
filePath=D:\\opensource\\ue-web\\src\\main\\webapp\\WEB-INF\\template\\html
templatePath=html_template.vm
五、控制器
package com.geenk.web.controller.generatehtml; import com.geenk.web.velocity_engine.GenerateHtmlUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap;
import java.util.Map; /**
* @author DUCHONG
* @since 2018-04-28 18:51
**/
@Controller
public class GenerateController { @Value("${filePath}")
private String filePath; @Value("${templatePath}")
private String templatePath; @ResponseBody
@RequestMapping(value = "/html",method = RequestMethod.GET)
public String generateHtml(){ //一般这里是数据库查出的记录的list,然后遍历list,逐个生成html,存放路径,一般是取 "配置+表字段值",作为存放的路径
//这里用for代替
for(int i=1;i<10;i++){ //页面要展示的数据
Map<String,Object> map=new HashMap<>();
map.put("title","news"+i);
map.put("userId",i);
map.put("userName","test"+i);
map.put("mobile","18106519020");
map.put("email","1427222829@qq.com"); GenerateHtmlUtil.generateHtmlByVelocity("news"+i,filePath,templatePath,map,"list");
} return "Over";
}
}
六、工具类
package com.geenk.web.velocity_engine; import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Properties; /**
* @author DUCHONG
* @since 2018-04-28 18:35
**/
public class GenerateHtmlUtil { static Logger logger = LoggerFactory.getLogger(GenerateHtmlUtil.class); /**
* 通过velocity 模板生成静态HTML 文件
* @param fileName 生成的文件的文件名称
* @param filePath 保存文件位置
* @param templatePath velocity模板文件路径
* @param params 集合
* @param pageName 页面上需要便利或者使用的变量,可以为任意值
*/
public static void generateHtmlByVelocity(String fileName, String filePath,
String templatePath, Object params, String pageName){ String finalFilePath=filePath+ File.separator+fileName+".html"; try { //设置加载模版文件的方式,在classpath 下面查找
Properties p = new Properties();
p.put("file.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(p); FileOutputStream fos = new FileOutputStream(finalFilePath);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
fos, "utf8"));
Template velocity_template = Velocity.getTemplate(templatePath,"utf8"); VelocityContext context = new VelocityContext();
context.put(pageName, params);
velocity_template.merge(context,writer);
writer.close(); }
catch (Exception e) {
logger.error("文件路径失败!",e);
}
} }
七、运行
浏览器输入localhost:8866/html,显示Over


Velocity根据模版生成静态html的更多相关文章
- Asp.net MVC Razor视图模版动态渲染PDF,Razor模版生成静态Html
Asp.net MVC Razor视图模版动态渲染PDF,Razor模版生成静态Html 1.前言 上一篇文章我开源了轮子,Asp.net Core 3.1 Razor视图模版动态渲染PDF,然后,很 ...
- 基于PHP生成静态页的实现方法
t1.php 复制代码 代码如下: <?php// 方法一根据模版生成静态页面// replaceTemplateString函数用于替换模板中指定字符串function replaceTemp ...
- 浅谈php生成静态页面
一.引 言 在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...
- .net 生成 静态页面
.net 生成 静态页面 <!--Main.Aspx--> <%@ page language="C#" %> <%@ import namespac ...
- FreeMarker 乱码解决方案 生成静态html文件
读取模板的时候有一个编码: Template template = this.tempConfiguration.getTemplate(templatePath,"UTF-8") ...
- 三种C#.net生成静态页面的方法
ASP.NET生成静态页面方法主要有三种 第一种方法:向服务器的动态页面发送请求,获取页面的html代码.这种方法缺点显而易见:速度慢.另外如果请求的动态页面有验证控件的话,返回的html页面却无 ...
- springmvc+freemarker生成静态html文件
参考资料: http://mylfd.iteye.com/blog/1896501 http://www.cnblogs.com/xxt19970908/p/5553045.html 个人实践: 1. ...
- java使用freemarker生成静态html页面
1. 模板文件static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...
- 减少服务器压力php生成静态xml文件
一.引 言 在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...
随机推荐
- Java执行过程
Java的运行原理 在Java中引入了虚拟机的概念,即在机器和编译程序之间加入了一层抽象的虚拟的机器.这台虚拟的机器在任何平台上都提供给编译程序一个的共同的接口.编译程序只需要面向虚拟机,生成虚拟机能 ...
- 目标检测 — 计算IOU
1.IOU=交集/并集 #include<iostream> #include<algorithm> #include<stdio.h> #include < ...
- python3中的unicode_escape
一. 响应的两种方式 在使用python3的requests模块时,发现获取响应有两种方式 其一,为文本响应内容, r.text 其二,为二进制响应内容,r.content 在<Python学习 ...
- RK30SDK开发板驱动分析(一):platform device 的概念与注册
做过51单片机或者ARM开发的人都知道,单片机内部都有自己的“片内外设”,比如UART,比如I2C,比如SPI等等... 写单片机程序的时候,比如对于UART的驱动,我们都是在程序中直接写一套函数,来 ...
- idea更换git地址操作
更换地址: git remote set-url origin XXXXXXXXXXXXXXX 查看远程地址: git remote -v
- Maven下载 || 配置本地仓库 || IntelliJ IDEA配置Maven教程
本文章主要介绍1.Maven下载 2.配置本地仓库Repository 3.IDEA配置Maven 三点. 相关博客: Eclipse配置Maven https://www.cnblogs.c ...
- 30 python 并发编程之多线程
一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 官网链接:https://docs.python ...
- JDK自动安装脚本
A:本脚本运行的机器,Linux B:待安装JDK的机器, Linux 首先在脚本运行的机器A上确定可以ssh无密码登录到待安装jdk的机器B上,然后就可以在A上运行本脚本: 代码如下: $ ./in ...
- hdu-2544-最短路(dijkstra算法模板)
题目链接 题意很清晰,入门级题目,适合各种模板,可用dijkstra, floyd, Bellman-ford, spfa Dijkstra链接 Floyd链接 Bellman-Ford链接 SPFA ...
- hdu1398 Square Coins(母函数)
题目类似于整数拆分,很明显用母函数来做. 母函数的写法基本固定,根据具体每项乘式的不同做出一些修改就行了.它的思路是从第一个括号开始,一个括号一个括号的乘开,用c1数组保存之前已经乘开的系数,即c1[ ...