- PropertyPlaceholderHelper
Springframework 提供的一个工具类,用于占位符的替换。
public class PlaceholderUtil {
public static String replace(String source, HashMap<String, String> dataMap) {
PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("{", "}");
return propertyPlaceholderHelper.replacePlaceholders(source, dataMap::get);
}
public static void main(String[] args) {
String str = "The IP is {ip_address}, and the host name is [{hostName}]";
HashMap<String, String> dataMap = new HashMap<>();
dataMap.put("ip_address", "127.0.0.1");
dataMap.put("hostName", "gw");
System.out.println(replace(str, dataMap));
}
}
- FreeMarkerTemplateUtils Springframework 提供的一个使用给定模型处理邮件模版的工具类,可将模版中的占位符进行替换。
public String processTemplate() throws TemplateException, IOException {
Template template = freemarkerConfigurer.createConfiguration().getTemplate("email.ftl");
Map<String, String> dataMap = ImmutableMap.of(
"username", "username",
"message", "It's a Fine Day."
);
return FreeMarkerTemplateUtils.processTemplateIntoString(template, dataMap);
}