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快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...
随机推荐
- audiojs 音频插件使用教程
audiojs 音频插件使用教程 github地址 https://kolber.github.io/audiojs/ 依赖文件 <script src="https://cdn.bo ...
- MySQL 体系结构和存储引擎
数据库: 物理操作系统文件或其他形式文件类型的集合 实例: MySQL数据库向后台线程以及一个共享内存区组成,共享内存可以被运行的后台线程所共享 MySQL 数据库实例在某统上的表现就是一个进程. M ...
- 使用Spring MVC表单标(转)
概述 在低版本的Spring中,你必须通过JSTL或<spring:bind>将表单对象绑定到HTML表单页面中,对于习惯了Struts表单标签的开发者来说,Spring MVC的 ...
- 16-THREE.JS 半球光
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- Tomcat 多端口访问多应用设置
目的 配置Tomcat,使用多端口访问不同应用 步骤 测试Tomcat版本为apache-tomcat-8.0.5,理论上支持7.0之上的版本 找到tomcat的主目录,打开conf文件夹,找到并打开 ...
- LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- dyci——IOS动态代码注入
有时候用xib,更改了布局需要重新运行才可以看到效果,对于比较复杂的应用尤其浪费时间,下面介绍一个工具dyci-不需要重Run应用,也能看到效果 yci的网址:https://github.com/D ...
- 2.mysql优化---增删改优化
整理自互联网 补充知识点:操作数据语句优化的认识 通常情况下,当访问某张表的时候,读取者首先必须获取该表的锁,如果有写入操作到达,那么写入者一直等待读取者完成操作(查询开始之后就不能中断,因此允许读取 ...
- How to install php 7.x on CentOS 7
Step 1: Setup the Webtatic YUM repo Precompiled PHP 7.x binaries are available for CentOS 7 from the ...
- String format方法的应用
String str=null; str=String.format("Hi,%s", "小超"); System.out.println(str); str= ...