master
王兵 5 years ago
parent ea0f7aa19c
commit 4ccc760629

@ -1,17 +1,12 @@
package com.example.frame.utils; package com.example.frame.utils;
import com.example.frame.auth.LocalData; 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.Configuration;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.util.ClassUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.io.FileOutputStream; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
@ -30,58 +25,97 @@ public class FreeMarkerUtil {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
// 初始化一些配置 // 初始化一些配置
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28); Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setDefaultEncoding("UTF-8"); cfg.setClassForTemplateLoading(FreeMarkerUtil.class, basePackagePath);
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);
freeMarkerConfigurer.setConfiguration(cfg); freeMarkerConfigurer.setConfiguration(cfg);
return freeMarkerConfigurer; 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) { public static Configuration getNewConfiguration(FreeMarkerConfigurer configurer) {
return configurer.getConfiguration(); 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 { try {
// 获取模板 // 创建字符流
Template template = defaultConfiguration.getTemplate(templatePath); Writer wr = new OutputStreamWriter(outputStream, "UTF-8");
// 输出文件路径 // 渲染
Writer wr = new OutputStreamWriter(new FileOutputStream(outPath), "UTF-8");
// 写入
template.process(model, wr); template.process(model, wr);
// 关闭流 // 关闭流
wr.close(); 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) { } catch (IOException e) {
e.printStackTrace(); 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(); 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 { try {
// 获取模板 // 获取模板
Template template = defaultConfiguration.getTemplate(templatePath); Template template = new Template(MD5Util.encode(source), source, getDefaultConfiguration());
// 输出文件路径 render(template, outputStream, model);
Writer wr = new OutputStreamWriter(outputStream, "UTF-8");
// 写入
template.process(model, wr);
// 关闭流
wr.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} }
} }
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.