|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
package com.example.frame.utils;
|
|
|
|
|
package ${domain}.frame.utils;
|
|
|
|
|
|
|
|
|
|
import com.example.frame.auth.LocalData;
|
|
|
|
|
import ${domain}.frame.auth.LocalData;
|
|
|
|
|
import freemarker.template.Configuration;
|
|
|
|
|
import freemarker.template.Template;
|
|
|
|
|
import freemarker.template.TemplateException;
|
|
|
|
@ -13,34 +13,51 @@ import java.io.OutputStreamWriter;
|
|
|
|
|
import java.io.Writer;
|
|
|
|
|
|
|
|
|
|
public class FreeMarkerUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取自动装配的Configuration,配置涉及视图层,谨慎修改!!!
|
|
|
|
|
*
|
|
|
|
|
* @return 配置
|
|
|
|
|
*/
|
|
|
|
|
public static Configuration getDefaultConfiguration() {
|
|
|
|
|
return LocalData.getBean(Configuration.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取自动装配配置工厂,一般用于创建独立Configuration,且复用配置
|
|
|
|
|
* 支持修改默认配置,并且不会影响视图层
|
|
|
|
|
*
|
|
|
|
|
* @return 配置工厂
|
|
|
|
|
*/
|
|
|
|
|
public static FreeMarkerConfigurer getDefaultConfigurer() {
|
|
|
|
|
return LocalData.getBean(FreeMarkerConfigurer.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static FreeMarkerConfigurer getNewConfigurer(String basePackagePath) {
|
|
|
|
|
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
|
|
|
|
|
// 初始化一些配置
|
|
|
|
|
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
|
|
|
|
|
cfg.setClassForTemplateLoading(FreeMarkerUtil.class, basePackagePath);
|
|
|
|
|
freeMarkerConfigurer.setConfiguration(cfg);
|
|
|
|
|
return freeMarkerConfigurer;
|
|
|
|
|
/**
|
|
|
|
|
* 获取复用系统配置的新配置对象
|
|
|
|
|
*
|
|
|
|
|
* @param basePackagePath 模板路径
|
|
|
|
|
* @return 配置
|
|
|
|
|
*/
|
|
|
|
|
public static Configuration getNewConfiguration(String basePackagePath) {
|
|
|
|
|
Configuration newConfiguration = getNewConfiguration();
|
|
|
|
|
newConfiguration.setClassForTemplateLoading(FreeMarkerUtil.class, basePackagePath);
|
|
|
|
|
return newConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取复用系统配置的新配置对象
|
|
|
|
|
*
|
|
|
|
|
* @return 配置
|
|
|
|
|
*/
|
|
|
|
|
public static Configuration getNewConfiguration() {
|
|
|
|
|
return getNewConfiguration("/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Configuration getNewConfiguration(String basePackagePath) {
|
|
|
|
|
FreeMarkerConfigurer newConfigurer = getNewConfigurer(basePackagePath);
|
|
|
|
|
return getNewConfiguration(newConfigurer);
|
|
|
|
|
Configuration configuration;
|
|
|
|
|
try {
|
|
|
|
|
configuration = getDefaultConfigurer().createConfiguration();
|
|
|
|
|
} catch (IOException | TemplateException e) {
|
|
|
|
|
configuration = new Configuration(Configuration.VERSION_2_3_28);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Configuration getNewConfiguration(FreeMarkerConfigurer configurer) {
|
|
|
|
|
return configurer.getConfiguration();
|
|
|
|
|
return configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -50,7 +67,7 @@ public class FreeMarkerUtil {
|
|
|
|
|
* @param outputStream 输出流
|
|
|
|
|
* @param model 数据模型
|
|
|
|
|
*/
|
|
|
|
|
public static void render(Template template, OutputStream outputStream, Object model) {
|
|
|
|
|
public static boolean render(Template template, OutputStream outputStream, Object model) {
|
|
|
|
|
try {
|
|
|
|
|
// 创建字符流
|
|
|
|
|
Writer wr = new OutputStreamWriter(outputStream, "UTF-8");
|
|
|
|
@ -58,30 +75,42 @@ public class FreeMarkerUtil {
|
|
|
|
|
template.process(model, wr);
|
|
|
|
|
// 关闭流
|
|
|
|
|
wr.close();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (IOException | TemplateException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void renderByResources(String resources, OutputStream outputStream, Object model) {
|
|
|
|
|
renderByResources(resources, true, outputStream, model);
|
|
|
|
|
/**
|
|
|
|
|
* 通过模板资源渲染
|
|
|
|
|
*
|
|
|
|
|
* @param resources 模板资源
|
|
|
|
|
* @param outputStream 输出流
|
|
|
|
|
* @param model 数据模型
|
|
|
|
|
*/
|
|
|
|
|
public static boolean renderByResources(String resources, OutputStream outputStream, Object model) {
|
|
|
|
|
return renderByResources(resources, true, outputStream, model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过模板资源渲染
|
|
|
|
|
*
|
|
|
|
|
* @param resources 模板资源
|
|
|
|
|
* @param defaultConfiguration 是否使用默认配置,与视图模板同目录同配置
|
|
|
|
|
* @param outputStream 输出流
|
|
|
|
|
* @param model 数据模型
|
|
|
|
|
*/
|
|
|
|
|
public static void renderByResources(String resources, boolean defaultConfigurer, OutputStream outputStream, Object model) {
|
|
|
|
|
Configuration newConfiguration = defaultConfigurer ? getDefaultConfiguration() : getNewConfiguration();
|
|
|
|
|
public static boolean renderByResources(String resources, boolean defaultConfiguration, OutputStream outputStream, Object model) {
|
|
|
|
|
Configuration newConfiguration = defaultConfiguration ? getDefaultConfiguration() : getNewConfiguration();
|
|
|
|
|
try {
|
|
|
|
|
// 构建模板
|
|
|
|
|
Template template = newConfiguration.getTemplate(resources);
|
|
|
|
|
render(template, outputStream, model);
|
|
|
|
|
// 渲染模板
|
|
|
|
|
return render(template, outputStream, model);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -92,13 +121,15 @@ public class FreeMarkerUtil {
|
|
|
|
|
* @param outputStream 输出流
|
|
|
|
|
* @param model 数据模型
|
|
|
|
|
*/
|
|
|
|
|
public static void renderByFile(File file, OutputStream outputStream, Object model) {
|
|
|
|
|
public static boolean renderByFile(File file, OutputStream outputStream, Object model) {
|
|
|
|
|
try {
|
|
|
|
|
// 构建模板
|
|
|
|
|
Template template = new Template(file.getName(), FileUtil.readFileToString(file), getDefaultConfiguration());
|
|
|
|
|
render(template, outputStream, model);
|
|
|
|
|
// 渲染模板
|
|
|
|
|
return render(template, outputStream, model);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -109,13 +140,15 @@ public class FreeMarkerUtil {
|
|
|
|
|
* @param outputStream 输出流
|
|
|
|
|
* @param model 数据模型
|
|
|
|
|
*/
|
|
|
|
|
public static void renderByString(String source, OutputStream outputStream, Object model) {
|
|
|
|
|
public static boolean renderByString(String source, OutputStream outputStream, Object model) {
|
|
|
|
|
try {
|
|
|
|
|
// 获取模板
|
|
|
|
|
// 构建模板
|
|
|
|
|
Template template = new Template(MD5Util.encode(source), source, getDefaultConfiguration());
|
|
|
|
|
render(template, outputStream, model);
|
|
|
|
|
// 渲染模板
|
|
|
|
|
return render(template, outputStream, model);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|