当前位置:网站首页>消息模板占位符的使用
消息模板占位符的使用
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则为替换的字段的值,用于消息模板的占位
边栏推荐
猜你喜欢
随机推荐
Golang协程调度器scheduler怎么使用
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!.
JVM运行时数据区与JMM内存模型是什么
以消费场景为驱动的CMDB要怎么建?
How to solve the dynamic binding of el-form-item prop attribute does not take effect
Are online account opening commissions reliable? Is online account opening safe?
tooltip control
Xingtu has been short of disruptive products?Will this M38T from the Qingdao factory be a breakthrough?
Topology Parts Disassembly 3D Visualization Solution
QT基础功能,信号、槽
后台管理系统的权限思路
Go GORM事务实例分析
电商库存系统的防超卖和高并发扣减方案
md5sum源码 可多平台编译
SQL的ROUND函数用法及其实例
hcip第九天
【无标题】setInterval和setTimeout详解
【翻译】CNCF培养的OpenMetrics成为一个孵化项目
EpiSci | Deep Reinforcement Learning for SoCs: Myth and Reality
COS 用户实践征文