模版引擎
//生成块jsp
for(CrfBlock block:page.getCrfBlocks()){
String blockName = "block"+block.getId();
blockName = blockName+ ".jsp";
freemark.setFileName(blockName);
freemark.setTemplateName("block.ftl"); //调用模版
t = null;
try {
t = freemark.getConfiguration().getTemplate(freemark.getTemplateName());
} catch (IOException e) {
e.printStackTrace();
}
outFile = new File(freemark.getFilePath()+ freemark.getFileName()); //输出文件
out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
fun.crfBlock(block, user, map, projectcrfservice);
try {
t.process(map, out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
package com.edc.util;
import java.io.File;
import java.io.IOException;
import freemarker.template.Configuration;
/**
* 使用freemark生成word
* @author stormwy
*
*/
public class Freemark {
/**
* freemark初始化
* @param templatePath 模板文件位置
* @throws IOException
*/
public Freemark(String templatePath) throws IOException {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setDirectoryForTemplateLoading(new File(templatePath));
}
/**
* freemark模板配置
*/
private Configuration configuration;
public Configuration getConfiguration() {
return configuration;
}
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}
/**
* freemark模板的名字
*/
private String templateName;
/**
* 生成文件名
*/
private String fileName;
/**
* 生成文件路径
*/
private String filePath;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getTemplateName() {
return templateName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
}