|
|
|
@ -149,8 +149,15 @@ public class FreeMarkerUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static boolean renderByFile(File file, Object model, Writer writer) {
|
|
|
|
|
try {
|
|
|
|
|
// 构建模板
|
|
|
|
|
Template template = new Template(file.getName(), FileUtil.readFileToString(file), getDefaultConfiguration());
|
|
|
|
|
// 模板名称
|
|
|
|
|
String name = MD5Util.encode(file.getAbsolutePath());
|
|
|
|
|
// 获取缓存模板
|
|
|
|
|
Template template = (Template) getDefaultConfiguration().getCacheStorage().get(name);
|
|
|
|
|
if (template == null) {
|
|
|
|
|
// 构建模板,此处构建的模板需手动缓存
|
|
|
|
|
template = new Template(file.getName(), FileUtil.readFileToString(file), getDefaultConfiguration());
|
|
|
|
|
getDefaultConfiguration().getCacheStorage().put(name, template);
|
|
|
|
|
}
|
|
|
|
|
// 渲染模板
|
|
|
|
|
return render(template, model, writer);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
@ -182,8 +189,15 @@ public class FreeMarkerUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static boolean renderByString(String source, Object model, Writer writer) {
|
|
|
|
|
try {
|
|
|
|
|
// 构建模板
|
|
|
|
|
Template template = new Template(MD5Util.encode(source), source, getDefaultConfiguration());
|
|
|
|
|
// 模板名称
|
|
|
|
|
String name = MD5Util.encode(source);
|
|
|
|
|
// 获取缓存模板
|
|
|
|
|
Template template = (Template) getDefaultConfiguration().getCacheStorage().get(name);
|
|
|
|
|
if (template == null) {
|
|
|
|
|
// 构建模板,此处构建的模板需手动缓存
|
|
|
|
|
template = new Template(name, source, getDefaultConfiguration());
|
|
|
|
|
getDefaultConfiguration().getCacheStorage().put(name, template);
|
|
|
|
|
}
|
|
|
|
|
// 渲染模板
|
|
|
|
|
return render(template, model, writer);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|