import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class VelocityGenerator {
protected VelocityEngine ve;
/**
* 准备velocity引擎
*/
public void init() {
ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
}
/**
* @param entity
* @param fileName /var/html/ xxx.html
* @throws IOException
*/
public void buildFileByTemplate(EntityObject entity, String fileName, String filePath) throws IOException {
//添加数据
VelocityContext ctx = new VelocityContext();
ctx.put("entity", entity);
//取得模板文件
Template domainModelTemplateA = ve.getTemplate("a_news.vm", "utf-8");
String outputPath = filePath + File.separator + "a_" + fileName;
//渲染并保存文件
render(domainModelTemplateA, ctx, outputPath);
outputPath = filePath + File.separator + "ot_" + fileName;
render(domainModelTemplateOt, ctx, outputPath);
}
/**
* 渲染
*
* @param template
* @param ctx
* @param file
*/
protected void render(Template template, VelocityContext ctx, String file) {
File out = new File(file);
FileWriter writer = null;
try {
//如果路径不存在,创建路径
out.getParentFile().mkdirs();
writer = new FileWriter(out);
//渲染
template.merge(ctx, writer);
writer.flush();
} catch (IOException e) {
logger.error("文件生成失败", e);
} finally {
try {
writer.close();
} catch (IOException e) {
logger.error("文件流关闭失败", e);
}
}
}
// public static void main(String[] args) throws IOException {
// VelocityGenerator velocityGenerator = new VelocityGenerator();
// velocityGenerator.init();
// EntityObject entityObject = new EntityObject();
// entityObject.setBody("51博客.");
// entityObject.setTitle("VelocityEngine使用");
// velocityGenerator.buildFileByTemplate(entityObject, "xxx.html", "/var");
// }
}
public class EntityObject {
private String title;
private String author;
private String body;
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
a_news.vm
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="keywords" content="your keywords">
<meta name="description" content="your description">
<meta name="author" content="author,email address">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no,email=no">
<title>VelocityEngine使用</title>
<!-- build:css static/css/act.css -->
<link rel="stylesheet" href="/static/css/core.css">
<link rel="stylesheet" href="/static/css/head.css">
<link rel="stylesheet" href="/static/css/icon.css">
<link rel="stylesheet" href="/static/css/otnews.css">
<!-- endbuild -->
</head>
<body>
<!--内容区域-->
<div class="service_txt">
${entity.body}
</div>
<!-- build:js static/js/act.js -->
<script src="/static/js/lib/zepto.min.js"></script>
<script src="/static/js/app_download.js"></script>
<!-- endbuild -->
</body>
</html>
转载于:https://blog.51cto.com/1385903/1875532