标签:控制 writer hash org 文件名 control static context ret
新公司的一个CMS项目要用到,这里记录下

<!-- 定义环境变量文件 -->
<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>
<!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">mobileNo</th>
<th class="jobs-title">email</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>
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


标签:控制 writer hash org 文件名 control static context ret
原文地址:https://www.cnblogs.com/geekdc/p/8970782.html