新公司的一个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

最新文章

  1. AutoFac在项目中的应用
  2. H5项目开发分享——用Canvas合成文字
  3. 51Nod 1428 活动安排问题
  4. c#文本转语音以及语音阅读小实例
  5. java并发编程:并发容器之CopyOnWriteArrayList(转)
  6. Maven搭建SpringMVC+Mybatis项目详解
  7. 将文件系统数据库迁移到ASM中
  8. php课程---随机数
  9. MemoryMappingFile泄漏分析过程
  10. php中mysqli 处理查询结果集的几个方法
  11. nau8822 codec driver 录音时mic bias 无法自动打开问题
  12. 【转】Android LCD(三):Samsung LCD接口篇
  13. Kill 正在执行的存储过程
  14. Linux桌面环境GNOME实用技巧
  15. DevOps教程
  16. 【ThinkPHP框架学习 】(1) --- thinkphp 3.2.3 验证码验证使用教程分享
  17. Xcode8 添加PCH文件
  18. PAT1088:Rational Arithmetic
  19. Scala之eq,equals,==的区别
  20. mysql的使用相关问题

热门文章

  1. review02
  2. [STL]vector与排序算法
  3. 安装SQL 2008失败 (win7 旗舰版 32位)
  4. Unity3D重要知识点(转)
  5. Webstorm设置Node.js智能提示
  6. python--关于if __name__==__main__的解释
  7. Idea_学习_10_Idea远程debug
  8. Python中类的约束
  9. stl_relops.h
  10. OpenCV - win7+vs2013(2012)+opencv3.0.0 环境配置 (以及配置技巧)