项目开发中,经常用到消息模板,其实实现很简单,先记录如下:
核心原理就是利用
StrSubstitutor的正则表达式对匹配的字符进行替换,它封装了由正则表达式灵活性带来的复杂性,从而以更简单,通用的方式组织数据。
1、简单的调用
- public static void main(String[] args) {
-
-
- Map
params = new HashMap<>(); - params.put("userName","闫从飞");
- params.put("cardNumber", "110996");
- params.put("buildingName","临沂山庄456");
- params.put("checkType", "1");
- params.put("redoReason", "非法集资");
-
- StrSubstitutor sub = new StrSubstitutor(params);
- String content = sub.replace("您好${userName},您卡号为${cardNumber} ,地址为${buildingName} 的 ${checkType} 审核业务,因 ${redoReason}被驳回,请注意查收!");
-
- System.out.println(content);
- SpringApplication.run ( JeeplusWebApplication.class, args );
- }
2、简单的调用-设置变量的默认值
- 字符串的变量如果在Map实体中找到对应的Key值,变量就不会被替换,而是直接以字符串展示。StrSubstitutor提供取值分隔符 ":-",添加到变量的后面。例如 ${undefined.number:-1234567890}.
-
- Map valuesMap = new HashMap();
- valuesMap.put("animal", "quick brown fox");
- valuesMap.put("target", "lazy dog");
-
- String templateString = "The ${animal} jumped over the ${target}. ${undefined.number:-1234567890}.";
-
- StrSubstitutor sub = new StrSubstitutor(valuesMap);
- String resolvedString = sub.replace(templateString);
-
- output
-
- The quick brown fox jumped over the lazy dog. 1234567890.
StrSubstitutor提供了setValueDelimiterMatcher(StrMatcher), setValueDelimiter(char) or setValueDelimiter(String)三种方法,可以自定义默认取值分隔符。
3 简单的调用-自定义匹配符
- 如果字符串中的变量形式不是 "${}",而是"$()",StrSubstitutor提供了不同的构造器以及setVariablePrefix(char),setVariableSuffix(char)等方法自定义匹配符。
-
- Map valuesMap = new HashMap();
- valuesMap.put("animal", "quick brown fox");
- valuesMap.put("target", "lazy dog");
-
- String templateString = "The &(animal) jumped over the &(target). &(undefined.number:-1234567890).";
-
- StrSubstitutor sub = new StrSubstitutor(valuesMap, "&(", ")", '&');
- String resolvedString = sub.replace(templateString);
-
- output
-
- The quick brown fox jumped over the lazy dog. 1234567890.
4 、一般的调用-自定义实体类
在系统中装载数据的实体有可能不是Map,而是其它的数据实体,例如com.fasterxml.jackson.databind.node.JsonNode。
- public class StrSubstitutorTest {
- @Test
- public void test() {
- StrSubstitutor substitutor = new StrSubstitutor();
- ObjectNode context = JsonNodeFactory.instance.objectNode();
- context.put("animal", "quick brown fox");
- context.put("target", "lazy dog");
-
- String templateString = "The ${animal} jumped over the ${target}. ${undefined.number:-1234567890}.";
-
- substitutor.setVariableResolver(new JsonPathContextLookup(context));
- String resolvedString = substitutor.replace(templateString);
-
- System.out.println(resolvedString);
- }
-
- class JsonPathContextLookup extends StrLookup
{ - private final JsonNode context;
-
- JsonPathContextLookup(JsonNode context) {
- super();
- this.context = context;
- }
-
- @Override
- public String lookup(String key) {
- return Optional.ofNullable(context.get(key)).map(JsonNode::asText).orElse(null);
- }
- }
- }
StrSubstitutor的使用非常的方便,简单,可以当作一个模板工具使用。该工具就就介绍完了,我觉得写博客是一个好习惯,它能加深自己对一个东西的理解。在一个系统中,如果看到一个未接触过的功能,刚开始接触时,往往会有一种心理压力,表现出畏难情绪,我认为这再正常不过了,毕竟人对未知不仅是充满了好奇,还有害怕。
坚持每个月至少两篇博客的习惯,多学习学习,思考思考总是好的。