当前位置:网站首页>消息模板占位符的使用
消息模板占位符的使用
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则为替换的字段的值,用于消息模板的占位
边栏推荐
猜你喜欢

Xingtu has been short of disruptive products?Will this M38T from the Qingdao factory be a breakthrough?

【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)

GRUB2的零日漏洞补丁现已推出

SQL的索引详细介绍

SQL窗口函数

阿里云的域名和ip绑定

频域分析实践介绍

Leetcode75. 颜色分类

Leetcode72. 编辑距离

How to use the Golang coroutine scheduler scheduler
随机推荐
以消费场景为驱动的CMDB要怎么建?
How to build a CMDB driven by consumption scenarios?
想做期货,农产品期货怎么炒?波动大么
千万级乘客排队系统重构&压测方案总结篇
频域分析实践介绍
Are online account opening commissions reliable? Is online account opening safe?
顺序表的简单描述及代码的简单实现
移动端吸顶方案
阿里云的域名和ip绑定
MySql 怎么查出符合条件的最新的数据行?
MySQL关系型数据库事务的ACID特性与实现方法
关于Mysql服务无法启动的问题
打开微信客服
Leetcode73. Matrix Zeroing
研发团队数字化转型实践
Go GORM事务实例分析
B011 - 基于51的多功能指纹智能锁
B001 - Intelligent ecological fish tank based on STM32
2022年SQL经典面试题总结(带解析)
MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?