FreeMarker模板

master
wangbing 5 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 ${domain}.frame.auth.LocalData;
import com.example.frame.mail.message.WFileMessage; import ${domain}.frame.mail.message.WFileMessage;
import com.example.frame.mail.message.WHtmlMessage; import ${domain}.frame.mail.message.WHtmlMessage;
import com.example.frame.mail.message.WInlineMessage; import ${domain}.frame.mail.message.WInlineMessage;
import com.example.frame.mail.message.WMessage; import ${domain}.frame.mail.message.WMessage;
import com.example.frame.mail.message.WTextMessage; import ${domain}.frame.mail.message.WTextMessage;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl; 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.io.File;
import java.util.ArrayList; import java.util.ArrayList;

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

@ -1,4 +1,4 @@
package com.example.frame.mail.message; package ${domain}.frame.mail.message;
import java.io.File; import java.io.File;
import java.util.HashMap; 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.Configuration;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
@ -13,34 +13,51 @@ import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
public class FreeMarkerUtil { public class FreeMarkerUtil {
/**
* Configuration
*
* @return
*/
public static Configuration getDefaultConfiguration() { public static Configuration getDefaultConfiguration() {
return LocalData.getBean(Configuration.class); return LocalData.getBean(Configuration.class);
} }
/**
* Configuration
*
*
* @return
*/
public static FreeMarkerConfigurer getDefaultConfigurer() { public static FreeMarkerConfigurer getDefaultConfigurer() {
return LocalData.getBean(FreeMarkerConfigurer.class); return LocalData.getBean(FreeMarkerConfigurer.class);
} }
public static FreeMarkerConfigurer getNewConfigurer(String basePackagePath) { /**
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); *
// 初始化一些配置 *
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28); * @param basePackagePath
cfg.setClassForTemplateLoading(FreeMarkerUtil.class, basePackagePath); * @return
freeMarkerConfigurer.setConfiguration(cfg); */
return freeMarkerConfigurer; public static Configuration getNewConfiguration(String basePackagePath) {
Configuration newConfiguration = getNewConfiguration();
newConfiguration.setClassForTemplateLoading(FreeMarkerUtil.class, basePackagePath);
return newConfiguration;
} }
/**
*
*
* @return
*/
public static Configuration getNewConfiguration() { public static Configuration getNewConfiguration() {
return getNewConfiguration("/"); Configuration configuration;
} try {
configuration = getDefaultConfigurer().createConfiguration();
public static Configuration getNewConfiguration(String basePackagePath) { } catch (IOException | TemplateException e) {
FreeMarkerConfigurer newConfigurer = getNewConfigurer(basePackagePath); configuration = new Configuration(Configuration.VERSION_2_3_28);
return getNewConfiguration(newConfigurer);
} }
return configuration;
public static Configuration getNewConfiguration(FreeMarkerConfigurer configurer) {
return configurer.getConfiguration();
} }
/** /**
@ -50,7 +67,7 @@ public class FreeMarkerUtil {
* @param outputStream * @param outputStream
* @param model * @param model
*/ */
public static void render(Template template, OutputStream outputStream, Object model) { public static boolean render(Template template, OutputStream outputStream, Object model) {
try { try {
// 创建字符流 // 创建字符流
Writer wr = new OutputStreamWriter(outputStream, "UTF-8"); Writer wr = new OutputStreamWriter(outputStream, "UTF-8");
@ -58,30 +75,42 @@ public class FreeMarkerUtil {
template.process(model, wr); template.process(model, wr);
// 关闭流 // 关闭流
wr.close(); wr.close();
return true;
} catch (IOException | TemplateException e) { } catch (IOException | TemplateException e) {
e.printStackTrace(); 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 resources
* @param defaultConfiguration 使
* @param outputStream * @param outputStream
* @param model * @param model
*/ */
public static void renderByResources(String resources, boolean defaultConfigurer, OutputStream outputStream, Object model) { public static boolean renderByResources(String resources, boolean defaultConfiguration, OutputStream outputStream, Object model) {
Configuration newConfiguration = defaultConfigurer ? getDefaultConfiguration() : getNewConfiguration(); Configuration newConfiguration = defaultConfiguration ? getDefaultConfiguration() : getNewConfiguration();
try { try {
// 构建模板 // 构建模板
Template template = newConfiguration.getTemplate(resources); Template template = newConfiguration.getTemplate(resources);
render(template, outputStream, model); // 渲染模板
return render(template, outputStream, model);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return false;
} }
} }
@ -92,13 +121,15 @@ public class FreeMarkerUtil {
* @param outputStream * @param outputStream
* @param model * @param model
*/ */
public static void renderByFile(File file, OutputStream outputStream, Object model) { public static boolean renderByFile(File file, OutputStream outputStream, Object model) {
try { try {
// 构建模板 // 构建模板
Template template = new Template(file.getName(), FileUtil.readFileToString(file), getDefaultConfiguration()); Template template = new Template(file.getName(), FileUtil.readFileToString(file), getDefaultConfiguration());
render(template, outputStream, model); // 渲染模板
return render(template, outputStream, model);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return false;
} }
} }
@ -109,13 +140,15 @@ public class FreeMarkerUtil {
* @param outputStream * @param outputStream
* @param model * @param model
*/ */
public static void renderByString(String source, OutputStream outputStream, Object model) { public static boolean renderByString(String source, OutputStream outputStream, Object model) {
try { try {
// 获取模板 // 构建模板
Template template = new Template(MD5Util.encode(source), source, getDefaultConfiguration()); Template template = new Template(MD5Util.encode(source), source, getDefaultConfiguration());
render(template, outputStream, model); // 渲染模板
return render(template, outputStream, model);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return false;
} }
} }
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.