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快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...
随机推荐
- 保护SSH的三把锁
///////////////////////////////写在前面//////////////////////////////////////原帖地址:http://www.ibm.com/dev ...
- 查看后台PHP进程(非PHP-FPM)
ps -ef | grep php | grep -v php-fpm
- BW信息STMS传输
项目时先接过来的.前面的人建了很多数据源,信息对象.我也建了不少.最后分成三个包.非常混乱,信息对象不在一个包里.导入报错.先后顺序也没有逻辑.可为难我了. 本来想着是全部释放掉,然后在传输连接收集自 ...
- MyEclipse设置默认的文档注释和背景色设置
- 《领域驱动设计 C# 2008 实现》 - 书摘精要
(P2) 智能客户反模式被 Eric Evans 定义为“把所有业务逻辑放进用户界面.把系统分解成小函数,作为分离的用户界面实现,并在里面嵌入业务规则.使用关系数据库作为共享的数据仓储.使用现有的自动 ...
- BEC translation exercise 1
U.S. oil drillers have made major efficiency improvements with a speed that has repeatedly surprised ...
- 11462 Age Sort(计数排序)
内存不够用,用计数排序可以解决问题. #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- 高可用-软件heartbeat的入门介绍
注:参考互联网整理. 一.简介Linux-HA的全称是High-Availability Linux,它是一个开源项目,这个开源项目的目标是:通过社区开发者的共同努力,提供一个增强linux可靠性(r ...
- 通过ifreme实现文件上传
模板页面添加ifreme <div style=' display: none;' > <iframe name ="uploadResponse_attachm ...
- BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)
题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ...