|
|
|
@ -1,17 +1,12 @@
|
|
|
|
|
package com.example.frame.utils;
|
|
|
|
|
|
|
|
|
|
import com.example.frame.auth.LocalData;
|
|
|
|
|
import freemarker.cache.ClassTemplateLoader;
|
|
|
|
|
import freemarker.cache.MultiTemplateLoader;
|
|
|
|
|
import freemarker.cache.TemplateLoader;
|
|
|
|
|
import freemarker.template.Configuration;
|
|
|
|
|
import freemarker.template.Template;
|
|
|
|
|
import freemarker.template.TemplateException;
|
|
|
|
|
import freemarker.template.TemplateExceptionHandler;
|
|
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
|
|
|
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
@ -30,58 +25,97 @@ public class FreeMarkerUtil {
|
|
|
|
|
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
|
|
|
|
|
// 初始化一些配置
|
|
|
|
|
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
|
|
|
|
|
cfg.setDefaultEncoding("UTF-8");
|
|
|
|
|
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
|
|
|
|
cfg.setNumberFormat("0.##");
|
|
|
|
|
cfg.setDateFormat("yyyy-MM-dd");
|
|
|
|
|
cfg.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
MultiTemplateLoader multiTemplateLoader = new MultiTemplateLoader(new TemplateLoader[]{
|
|
|
|
|
new ClassTemplateLoader(ClassUtils.getDefaultClassLoader(), basePackagePath),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cfg.setTemplateLoader(multiTemplateLoader);
|
|
|
|
|
cfg.setClassForTemplateLoading(FreeMarkerUtil.class, basePackagePath);
|
|
|
|
|
freeMarkerConfigurer.setConfiguration(cfg);
|
|
|
|
|
return freeMarkerConfigurer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Configuration getNewConfiguration() {
|
|
|
|
|
return getNewConfiguration("/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Configuration getNewConfiguration(String basePackagePath) {
|
|
|
|
|
FreeMarkerConfigurer newConfigurer = getNewConfigurer(basePackagePath);
|
|
|
|
|
return getNewConfiguration(newConfigurer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Configuration getNewConfiguration(FreeMarkerConfigurer configurer) {
|
|
|
|
|
return configurer.getConfiguration();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void outFile(String templatePath, String outPath, Object model) {
|
|
|
|
|
Configuration defaultConfiguration = getDefaultConfiguration();
|
|
|
|
|
/**
|
|
|
|
|
* 通过模板渲染
|
|
|
|
|
*
|
|
|
|
|
* @param template 模板
|
|
|
|
|
* @param outputStream 输出流
|
|
|
|
|
* @param model 数据模型
|
|
|
|
|
*/
|
|
|
|
|
public static void render(Template template, OutputStream outputStream, Object model) {
|
|
|
|
|
try {
|
|
|
|
|
// 获取模板
|
|
|
|
|
Template template = defaultConfiguration.getTemplate(templatePath);
|
|
|
|
|
// 输出文件路径
|
|
|
|
|
Writer wr = new OutputStreamWriter(new FileOutputStream(outPath), "UTF-8");
|
|
|
|
|
// 写入
|
|
|
|
|
// 创建字符流
|
|
|
|
|
Writer wr = new OutputStreamWriter(outputStream, "UTF-8");
|
|
|
|
|
// 渲染
|
|
|
|
|
template.process(model, wr);
|
|
|
|
|
// 关闭流
|
|
|
|
|
wr.close();
|
|
|
|
|
} catch (IOException | TemplateException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void renderByResources(String resources, OutputStream outputStream, Object model) {
|
|
|
|
|
renderByResources(resources, true, outputStream, model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过模板资源渲染
|
|
|
|
|
*
|
|
|
|
|
* @param resources 模板资源
|
|
|
|
|
* @param outputStream 输出流
|
|
|
|
|
* @param model 数据模型
|
|
|
|
|
*/
|
|
|
|
|
public static void renderByResources(String resources, boolean defaultConfigurer, OutputStream outputStream, Object model) {
|
|
|
|
|
Configuration newConfiguration = defaultConfigurer ? getDefaultConfiguration() : getNewConfiguration();
|
|
|
|
|
try {
|
|
|
|
|
// 构建模板
|
|
|
|
|
Template template = newConfiguration.getTemplate(resources);
|
|
|
|
|
render(template, outputStream, model);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (TemplateException e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过模板文件渲染
|
|
|
|
|
*
|
|
|
|
|
* @param file 模板文件
|
|
|
|
|
* @param outputStream 输出流
|
|
|
|
|
* @param model 数据模型
|
|
|
|
|
*/
|
|
|
|
|
public static void renderByFile(File file, OutputStream outputStream, Object model) {
|
|
|
|
|
try {
|
|
|
|
|
// 构建模板
|
|
|
|
|
Template template = new Template(file.getName(), FileUtil.readFileToString(file), getDefaultConfiguration());
|
|
|
|
|
render(template, outputStream, model);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void outOutput(String templatePath, OutputStream outputStream, Object model) {
|
|
|
|
|
Configuration defaultConfiguration = getDefaultConfiguration();
|
|
|
|
|
/**
|
|
|
|
|
* 通过模板字符串渲染
|
|
|
|
|
*
|
|
|
|
|
* @param source 模板字符串
|
|
|
|
|
* @param outputStream 输出流
|
|
|
|
|
* @param model 数据模型
|
|
|
|
|
*/
|
|
|
|
|
public static void renderByString(String source, OutputStream outputStream, Object model) {
|
|
|
|
|
try {
|
|
|
|
|
// 获取模板
|
|
|
|
|
Template template = defaultConfiguration.getTemplate(templatePath);
|
|
|
|
|
// 输出文件路径
|
|
|
|
|
Writer wr = new OutputStreamWriter(outputStream, "UTF-8");
|
|
|
|
|
// 写入
|
|
|
|
|
template.process(model, wr);
|
|
|
|
|
// 关闭流
|
|
|
|
|
wr.close();
|
|
|
|
|
Template template = new Template(MD5Util.encode(source), source, getDefaultConfiguration());
|
|
|
|
|
render(template, outputStream, model);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (TemplateException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|