• java高手进阶之:消息模板


    项目开发中,经常用到消息模板,其实实现很简单,先记录如下:

    核心原理就是利用

    StrSubstitutor的正则表达式对匹配的字符进行替换,它封装了由正则表达式灵活性带来的复杂性,从而以更简单,通用的方式组织数据。

    1、简单的调用

    1. public static void main(String[] args) {
    2. Map params = new HashMap<>();
    3. params.put("userName","闫从飞");
    4. params.put("cardNumber", "110996");
    5. params.put("buildingName","临沂山庄456");
    6. params.put("checkType", "1");
    7. params.put("redoReason", "非法集资");
    8. StrSubstitutor sub = new StrSubstitutor(params);
    9. String content = sub.replace("您好${userName},您卡号为${cardNumber} ,地址为${buildingName} 的 ${checkType} 审核业务,因 ${redoReason}被驳回,请注意查收!");
    10. System.out.println(content);
    11. SpringApplication.run ( JeeplusWebApplication.class, args );
    12. }

    2、简单的调用-设置变量的默认值

    1. 字符串的变量如果在Map实体中找到对应的Key值,变量就不会被替换,而是直接以字符串展示。StrSubstitutor提供取值分隔符 ":-",添加到变量的后面。例如 ${undefined.number:-1234567890}.
    2. Map valuesMap = new HashMap();
    3. valuesMap.put("animal", "quick brown fox");
    4. valuesMap.put("target", "lazy dog");
    5. String templateString = "The ${animal} jumped over the ${target}. ${undefined.number:-1234567890}.";
    6. StrSubstitutor sub = new StrSubstitutor(valuesMap);
    7. String resolvedString = sub.replace(templateString);
    8. output
    9. The quick brown fox jumped over the lazy dog. 1234567890.

     StrSubstitutor提供了setValueDelimiterMatcher(StrMatcher), setValueDelimiter(char) or setValueDelimiter(String)三种方法,可以自定义默认取值分隔符

    3 简单的调用-自定义匹配符

    1. 如果字符串中的变量形式不是 "${}",而是"$()",StrSubstitutor提供了不同的构造器以及setVariablePrefix(char),setVariableSuffix(char)等方法自定义匹配符。
    2. Map valuesMap = new HashMap();
    3. valuesMap.put("animal", "quick brown fox");
    4. valuesMap.put("target", "lazy dog");
    5. String templateString = "The &(animal) jumped over the &(target). &(undefined.number:-1234567890).";
    6. StrSubstitutor sub = new StrSubstitutor(valuesMap, "&(", ")", '&');
    7. String resolvedString = sub.replace(templateString);
    8. output
    9. The quick brown fox jumped over the lazy dog. 1234567890.

    4 、一般的调用-自定义实体类

    在系统中装载数据的实体有可能不是Map,而是其它的数据实体,例如com.fasterxml.jackson.databind.node.JsonNode。

    1. public class StrSubstitutorTest {
    2. @Test
    3. public void test() {
    4. StrSubstitutor substitutor = new StrSubstitutor();
    5. ObjectNode context = JsonNodeFactory.instance.objectNode();
    6. context.put("animal", "quick brown fox");
    7. context.put("target", "lazy dog");
    8. String templateString = "The ${animal} jumped over the ${target}. ${undefined.number:-1234567890}.";
    9. substitutor.setVariableResolver(new JsonPathContextLookup(context));
    10. String resolvedString = substitutor.replace(templateString);
    11. System.out.println(resolvedString);
    12. }
    13. class JsonPathContextLookup extends StrLookup {
    14. private final JsonNode context;
    15. JsonPathContextLookup(JsonNode context) {
    16. super();
    17. this.context = context;
    18. }
    19. @Override
    20. public String lookup(String key) {
    21. return Optional.ofNullable(context.get(key)).map(JsonNode::asText).orElse(null);
    22. }
    23. }
    24. }

         StrSubstitutor的使用非常的方便,简单,可以当作一个模板工具使用。该工具就就介绍完了,我觉得写博客是一个好习惯,它能加深自己对一个东西的理解。在一个系统中,如果看到一个未接触过的功能,刚开始接触时,往往会有一种心理压力,表现出畏难情绪,我认为这再正常不过了,毕竟人对未知不仅是充满了好奇,还有害怕。

           坚持每个月至少两篇博客的习惯,多学习学习,思考思考总是好的。
     

  • 相关阅读:
    半量化交易(一)
    MAE
    CMake 基础学习
    【C刷题训练营】第三讲(c语言入门训练)
    黑马React18: 基础Part 1
    基于C#+MySQL的“零售店”商品销售与管理系统
    Kubernetes_17_新建用户账号UserAccount(实践类)
    CVE-2021-3156 漏洞复现笔记
    基于Python的QQ音乐音频图片搜索系统
    Linux常用命令知识点大全(1)
  • 原文地址:https://blog.csdn.net/gaowenhui2008/article/details/133947557