记录项目中接触新知识:
mustcache 模板语法
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
/** * @param templateText 传入类似于: {{key}}超限告警 * @param params map 对象 ,用来匹配模板文字 * @return */ public static String mustacheMonitorTemplate(String templateText, Map<String, Object> params) { String result = templateText; if (!StringUtil.isEmpty(templateText) && templateText.indexOf("{{") >= 0) { MustacheFactory mustacheFactory = new DefaultMustacheFactory();
// mustcacheFactory.compile()方法第二个参数为定义改模板的name,自定义 Mustache mustache = mustacheFactory.compile(new StringReader(templateText), "template"); StringWriter writer = new StringWriter(); try { mustache.execute(writer, params).flush(); } catch (Throwable t) { writer.write(EVENT_NAME); logger.error(" fill monitor template \"" + templateText + "\" failed: " + t.toString(), t); } result = writer.toString().replaceAll("%",""); } return result; }
public static void main(String[] args) {
Map<String,Object> map = new HashMap<>();
map.put("dd","东夷城四顾剑");
map.put("ee","姑苏慕容复");
String str = mustacheMonitorTemplate("{{dd}}大帅比",map,null);
System.out.println(str);
}