FreeMarker模板

master
wangbing 4 years ago
parent 4ccc760629
commit 625b6e0ee5

@ -1,11 +1,11 @@
package com.example.frame.mail;
package ${domain}.frame.mail;
import com.example.frame.auth.LocalData;
import com.example.frame.mail.message.WFileMessage;
import com.example.frame.mail.message.WHtmlMessage;
import com.example.frame.mail.message.WInlineMessage;
import com.example.frame.mail.message.WMessage;
import com.example.frame.mail.message.WTextMessage;
import ${domain}.frame.auth.LocalData;
import ${domain}.frame.mail.message.WFileMessage;
import ${domain}.frame.mail.message.WHtmlMessage;
import ${domain}.frame.mail.message.WInlineMessage;
import ${domain}.frame.mail.message.WMessage;
import ${domain}.frame.mail.message.WTextMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@ -1,4 +1,4 @@
package com.example.frame.mail.message;
package ${domain}.frame.mail.message;
import java.io.File;
import java.util.ArrayList;

@ -1,4 +1,4 @@
package com.example.frame.mail.message;
package ${domain}.frame.mail.message;
/**
* Html

@ -1,4 +1,4 @@
package com.example.frame.mail.message;
package ${domain}.frame.mail.message;
import java.io.File;
import java.util.HashMap;

@ -1,4 +1,4 @@
package com.example.frame.mail.message;
package ${domain}.frame.mail.message;
/**
*

@ -1,4 +1,4 @@
package com.example.frame.mail.message;
package ${domain}.frame.mail.message;
/**
*

@ -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;
}
public static Configuration getNewConfiguration() {
return getNewConfiguration("/");
}
/**
*
*
* @param basePackagePath
* @return
*/
public static Configuration getNewConfiguration(String basePackagePath) {
FreeMarkerConfigurer newConfigurer = getNewConfigurer(basePackagePath);
return getNewConfiguration(newConfigurer);
Configuration newConfiguration = getNewConfiguration();
newConfiguration.setClassForTemplateLoading(FreeMarkerUtil.class, basePackagePath);
return newConfiguration;
}
public static Configuration getNewConfiguration(FreeMarkerConfigurer configurer) {
return configurer.getConfiguration();
/**
*
*
* @return
*/
public static Configuration getNewConfiguration() {
Configuration configuration;
try {
configuration = getDefaultConfigurer().createConfiguration();
} catch (IOException | TemplateException e) {
configuration = new Configuration(Configuration.VERSION_2_3_28);
}
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,15 +75,13 @@ 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);
}
/**
*
*
@ -74,14 +89,28 @@ public class FreeMarkerUtil {
* @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, OutputStream outputStream, Object model) {
return renderByResources(resources, true, outputStream, model);
}
/**
*
*
* @param resources
* @param defaultConfiguration 使
* @param outputStream
* @param model
*/
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;
}
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.