当前位置:网站首页>消息模板占位符的使用
消息模板占位符的使用
2022-08-01 18:01:00 【勉之~】
消息模板占位符的使用
一 具体实现
说明:根据@占位符进去匹配占位
案例:“模板名称:@[email protected],发生了变更,变更描述:@[email protected]的变更描述是:@[email protected]”;
1. 占位符解析
//解析占位符拼接消息模板
public String getContent(MsgType msgType, Map<String, String> messageAttrMap) {
//获取带占位符的消息模板
String result = messageTypeRepo.getByCode(msgType.name()).getContentTemplate();
//result = "模板名称:@[email protected],发生了变更,变更描述:@[email protected]的变更描述是:@[email protected]";
Pattern pattern = Pattern.compile("@(.*?)@");
Matcher matcher = pattern.matcher(result);
while (matcher.find()) {
//占位符
String placeholder = matcher.group();
String key = placeholder.substring(1, placeholder.length() - 1);
if (messageAttrMap.containsKey(key)) {
String value = "null";
if (Objects.nonNull(messageAttrMap.get(key))) {
value = messageAttrMap.get(key).trim();
}
result = StringUtils.replaceAll(result, placeholder, value);
// System.out.println(result);
}
}
return result;
}
2. 占位符参数的赋值
@ApiModelProperty(value = "消息模板类型",required = true)
private MsgType msgType;
@ApiModelProperty(value = "消息内容的属性",required = true)
private Map<String, String> msgAttrMap;//key可能有重复的属性字段,需要替换,value则为替换的字段的值,用于消息模板的占位
边栏推荐
- ACID Characteristics and Implementation Methods of MySQL Relational Database Transactions
- 一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
- When custom annotations implement log printing, specific fields are blocked from printing
- MySQL 45 Talk | 09 How to choose common index and unique index?
- C language theory--a solid foundation for the written test and interview
- el-form-item prop属性动态绑定不生效如何解决
- 千万级乘客排队系统重构&压测方案总结篇
- opencv syntax Mat type summary
- 极化微波成像概述3
- Solve the problem that MySQL cannot insert Chinese data
猜你喜欢
随机推荐
How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
opencv基本的图像处理
C#/VB.NET:从 PDF 文档中提取所有表格
QT基础功能,信号、槽
顺序表的简单描述及代码的简单实现
【Day_11 0506】 最近公共祖先
Shell nl命令详解(显示行号、读取文件)
小贝拉机器人是朋友_普渡科技召开新品发布会,新一代送餐机器人“贝拉”温暖登场...
[供应链·案例篇]石油和天然气行业的数字化转型用例
三维空间中点的插值
基于BiGRU和GAN的数据生成方法
Leetcode75. 颜色分类
ExcelPatternTool: Excel表格-数据库互导工具
【100个网络运维工作者必须知道的小知识!】
Go GORM事务实例分析
hcip第九天
公用函数----mfc
typora操作手册
Are online account opening commissions reliable? Is online account opening safe?
以消费场景为驱动的CMDB要怎么建?