参考文章
开始
用SpringBoot做一个简易前台页面,其中左侧的菜单是fsLayui的实现的,需要对它进行国际化。方案就是后台定一个接口,返回json的配置信息,加上菜单配置项。但是菜单的字符串太长了,如果使用拼接的方式书写很麻烦,所以想到定义一个模板数据,使用正则表达式去匹配并替换对应的数据。
如何实现
java.util.regex.Matcher
提供了两个方法appendReplacement
和appendTail
,用来通过StringBuffer来替换和拼接替换以后的字符串。
示例java代码
final String menuTemplate = "{\n" +
" \"errorNo\":0,\n" +
" \"results\":\n" +
" {\n" +
" \"data\":\n" +
" [\n" +
"\t\t{\"menuId\":\"1\",\"menuName\":\"##menu.console##\",\"menuIcon\":\"fa-cog\",\"menuHref\":\"\",\"parentMenuId\":\"0\"},\n" +
"\t\t{\"menuId\":\"11\",\"menuName\":\"充值业务\",\"menuIcon\":\"fa-dollar\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"111\",\"menuName\":\"服务介绍\",\"menuIcon\":\"\",\"menuHref\":\"views/home/index.html\",\"parentMenuId\":\"11\"},\n" +
"\t\t{\"menuId\":\"112\",\"menuName\":\"充值服务\",\"menuIcon\":\"\",\"menuHref\":\"views/rechargeServer/index\",\"parentMenuId\":\"11\"},\n" +
"\t\t{\"menuId\":\"113\",\"menuName\":\"额度查询\",\"menuIcon\":\"\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"11\"},\n" +
"\t\t{\"menuId\":\"114\",\"menuName\":\"冲正服务\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/treeDatagrid/index.html\",\"parentMenuId\":\"11\"},\n" +
"\t\t{\"menuId\":\"115\",\"menuName\":\"撤销服务\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/multiDatagrid/index.html\",\"parentMenuId\":\"11\"},\n" +
"\t\t{\"menuId\":\"12\",\"menuName\":\"清算服务\",\"menuIcon\":\"fa-retweet\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"121\",\"menuName\":\"分销商清算\",\"menuIcon\":\"fa-list\",\"menuHref\":\"/sys/views/datagrid/index\",\"parentMenuId\":\"12\"},\n" +
"\t\t{\"menuId\":\"122\",\"menuName\":\"终端清算\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"12\"},\n" +
"\t\t{\"menuId\":\"123\",\"menuName\":\"清算设置\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"12\"},\n" +
"\t\t{\"menuId\":\"124\",\"menuName\":\"清算报表\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"12\"},\n" +
"\t\t{\"menuId\":\"13\",\"menuName\":\"终端管理\",\"menuIcon\":\"fa-microchip\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"131\",\"menuName\":\"终端注册\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid/index.html\",\"parentMenuId\":\"13\"},\n" +
"\t\t{\"menuId\":\"132\",\"menuName\":\"终端状态\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/posDevice/deviceList\",\"parentMenuId\":\"13\"},\n" +
"\t\t{\"menuId\":\"133\",\"menuName\":\"终端注销\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"13\"},\n" +
"\t\t{\"menuId\":\"134\",\"menuName\":\"终端升级\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"13\"},\n" +
"\t\t{\"menuId\":\"14\",\"menuName\":\"分销系统\",\"menuIcon\":\"fa-sitemap\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"143\",\"menuName\":\"分销商列表\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/distribution/list\",\"parentMenuId\":\"14\"},\n" +
"\t\t{\"menuId\":\"141\",\"menuName\":\"新增分销商\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/distribution/add.html\",\"parentMenuId\":\"14\"},\n" +
"\t\t{\"menuId\":\"142\",\"menuName\":\"分销商审核\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/distribution/examine.html\",\"parentMenuId\":\"14\"},\n" +
"\t\t{\"menuId\":\"144\",\"menuName\":\"分销商评级\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/distribution/rating.html\",\"parentMenuId\":\"14\"},\n" +
"\t\t{\"menuId\":\"15\",\"menuName\":\"POS账号管理\",\"menuIcon\":\"fa-user-circle-o\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"151\",\"menuName\":\"新增管理账号\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid/index.html\",\"parentMenuId\":\"15\"},\n" +
"\t\t{\"menuId\":\"152\",\"menuName\":\"新增分销商账号\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"15\"},\n" +
"\t\t{\"menuId\":\"153\",\"menuName\":\"账号信息管理\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"15\"},\n" +
"\t\t{\"menuId\":\"154\",\"menuName\":\"账号注销\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"15\"},\n" +
"\t\t{\"menuId\":\"16\",\"menuName\":\"推送管理\",\"menuIcon\":\"fa-user-circle-o\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"161\",\"menuName\":\"推送内容管理\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid/index.html\",\"parentMenuId\":\"16\"},\n" +
"\t\t{\"menuId\":\"162\",\"menuName\":\"推送设置\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"16\"},\n" +
"\t\t{\"menuId\":\"17\",\"menuName\":\"操作日志\",\"menuIcon\":\"fa-bars\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"171\",\"menuName\":\"充值日志\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/logs/rechargelog.html\",\"parentMenuId\":\"17\"},\n" +
"\t\t{\"menuId\":\"172\",\"menuName\":\"POS登录日志\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/logs/loginlog.html\",\"parentMenuId\":\"17\"},\n" +
"\t\t{\"menuId\":\"175\",\"menuName\":\"POS管理日志\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/logs/sysusercontrolog.html\",\"parentMenuId\":\"17\"},\n" +
"\t\t{\"menuId\":\"174\",\"menuName\":\"系统登录日志\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/logs/loginlog.html\",\"parentMenuId\":\"17\"},\n" +
"\t\t{\"menuId\":\"173\",\"menuName\":\"系统管理日志\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/logs/posusercontrolog.html\",\"parentMenuId\":\"17\"},\n" +
"\t\t{\"menuId\":\"176\",\"menuName\":\"风险控制日志\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/logs/index.html\",\"parentMenuId\":\"17\"},\n" +
"\t\t{\"menuId\":\"18\",\"menuName\":\"数据分析\",\"menuIcon\":\"fa-bar-chart\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"184\",\"menuName\":\"总体概览\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/analysis/index.html\",\"parentMenuId\":\"18\"},\n" +
"\t\t{\"menuId\":\"181\",\"menuName\":\"日度分析\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/analysis/dayanalysis.html\",\"parentMenuId\":\"18\"},\n" +
"\t\t{\"menuId\":\"182\",\"menuName\":\"月度分析\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/analysis/monthanalysis.html\",\"parentMenuId\":\"18\"},\n" +
"\t\t{\"menuId\":\"183\",\"menuName\":\"年度分析\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/analysis/yearanalysis.html\",\"parentMenuId\":\"18\"},\n" +
"\t\t{\"menuId\":\"19\",\"menuName\":\"风险控制\",\"menuIcon\":\"fa-shield\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"191\",\"menuName\":\"终端风险\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid/index.html\",\"parentMenuId\":\"19\"},\n" +
"\t\t{\"menuId\":\"192\",\"menuName\":\"分销商风险\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"19\"},\n" +
"\t\t{\"menuId\":\"193\",\"menuName\":\"账户风险\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"19\"},\n" +
"\t\t{\"menuId\":\"194\",\"menuName\":\"风险预警规则\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"19\"},\n" +
"\t\t{\"menuId\":\"20\",\"menuName\":\"系统管理\",\"menuIcon\":\"fa-cog\",\"menuHref\":\"\",\"parentMenuId\":\"1\"},\n" +
"\t\t{\"menuId\":\"201\",\"menuName\":\"界面管理\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid/index.html\",\"parentMenuId\":\"20\"},\n" +
"\t\t{\"menuId\":\"202\",\"menuName\":\"访问控制\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"20\"},\n" +
"\t\t{\"menuId\":\"203\",\"menuName\":\"系统公告\",\"menuIcon\":\"fa-list\",\"menuHref\":\"views/datagrid2/index.html\",\"parentMenuId\":\"20\"}\n" +
" ]\n" +
" }\n" +
"}";
Map<String, String> contentMap = new HashMap<>();
contentMap.put("menu.console", "控制台");
StringBuffer menuResultBuilder = new StringBuffer();
Pattern pattern = Pattern.compile("##[a-zA-Z\\.]+##");
Matcher matcher = pattern.matcher(menuTemplate);
while(matcher.find()) {
String group = matcher.group();
String key = group.substring(2, group.length() - 2);
String value = contentMap.get(key);
matcher.appendReplacement(menuResultBuilder, value);
System.out.println("-----中间结果-----");
System.out.println(menuResultBuilder.toString());
}
matcher.appendTail(menuResultBuilder);
System.out.println("-----结果-----");
System.out.println(menuResultBuilder.toString());
输出结果
-----中间结果-----
{
"errorNo":0,
"results":
{
"data":
[
{"menuId":"1","menuName":"控制台
-----结果-----
{
"errorNo":0,
"results":
{
"data":
[
{"menuId":"1","menuName":"控制台","menuIcon":"fa-cog","menuHref":"","parentMenuId":"0"},
{"menuId":"11","menuName":"充值业务","menuIcon":"fa-dollar","menuHref":"","parentMenuId":"1"},
{"menuId":"111","menuName":"服务介绍","menuIcon":"","menuHref":"views/home/index.html","parentMenuId":"11"},
{"menuId":"112","menuName":"充值服务","menuIcon":"","menuHref":"views/rechargeServer/index","parentMenuId":"11"},
{"menuId":"113","menuName":"额度查询","menuIcon":"","menuHref":"views/datagrid2/index.html","parentMenuId":"11"},
{"menuId":"114","menuName":"冲正服务","menuIcon":"fa-list","menuHref":"views/treeDatagrid/index.html","parentMenuId":"11"},
{"menuId":"115","menuName":"撤销服务","menuIcon":"fa-list","menuHref":"views/multiDatagrid/index.html","parentMenuId":"11"},
{"menuId":"12","menuName":"清算服务","menuIcon":"fa-retweet","menuHref":"","parentMenuId":"1"},
{"menuId":"121","menuName":"分销商清算","menuIcon":"fa-list","menuHref":"/sys/views/datagrid/index","parentMenuId":"12"},
{"menuId":"122","menuName":"终端清算","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"12"},
{"menuId":"123","menuName":"清算设置","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"12"},
{"menuId":"124","menuName":"清算报表","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"12"},
{"menuId":"13","menuName":"终端管理","menuIcon":"fa-microchip","menuHref":"","parentMenuId":"1"},
{"menuId":"131","menuName":"终端注册","menuIcon":"fa-list","menuHref":"views/datagrid/index.html","parentMenuId":"13"},
{"menuId":"132","menuName":"终端状态","menuIcon":"fa-list","menuHref":"views/posDevice/deviceList","parentMenuId":"13"},
{"menuId":"133","menuName":"终端注销","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"13"},
{"menuId":"134","menuName":"终端升级","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"13"},
{"menuId":"14","menuName":"分销系统","menuIcon":"fa-sitemap","menuHref":"","parentMenuId":"1"},
{"menuId":"143","menuName":"分销商列表","menuIcon":"fa-list","menuHref":"views/distribution/list","parentMenuId":"14"},
{"menuId":"141","menuName":"新增分销商","menuIcon":"fa-list","menuHref":"views/distribution/add.html","parentMenuId":"14"},
{"menuId":"142","menuName":"分销商审核","menuIcon":"fa-list","menuHref":"views/distribution/examine.html","parentMenuId":"14"},
{"menuId":"144","menuName":"分销商评级","menuIcon":"fa-list","menuHref":"views/distribution/rating.html","parentMenuId":"14"},
{"menuId":"15","menuName":"POS账号管理","menuIcon":"fa-user-circle-o","menuHref":"","parentMenuId":"1"},
{"menuId":"151","menuName":"新增管理账号","menuIcon":"fa-list","menuHref":"views/datagrid/index.html","parentMenuId":"15"},
{"menuId":"152","menuName":"新增分销商账号","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"15"},
{"menuId":"153","menuName":"账号信息管理","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"15"},
{"menuId":"154","menuName":"账号注销","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"15"},
{"menuId":"16","menuName":"推送管理","menuIcon":"fa-user-circle-o","menuHref":"","parentMenuId":"1"},
{"menuId":"161","menuName":"推送内容管理","menuIcon":"fa-list","menuHref":"views/datagrid/index.html","parentMenuId":"16"},
{"menuId":"162","menuName":"推送设置","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"16"},
{"menuId":"17","menuName":"操作日志","menuIcon":"fa-bars","menuHref":"","parentMenuId":"1"},
{"menuId":"171","menuName":"充值日志","menuIcon":"fa-list","menuHref":"views/logs/rechargelog.html","parentMenuId":"17"},
{"menuId":"172","menuName":"POS登录日志","menuIcon":"fa-list","menuHref":"views/logs/loginlog.html","parentMenuId":"17"},
{"menuId":"175","menuName":"POS管理日志","menuIcon":"fa-list","menuHref":"views/logs/sysusercontrolog.html","parentMenuId":"17"},
{"menuId":"174","menuName":"系统登录日志","menuIcon":"fa-list","menuHref":"views/logs/loginlog.html","parentMenuId":"17"},
{"menuId":"173","menuName":"系统管理日志","menuIcon":"fa-list","menuHref":"views/logs/posusercontrolog.html","parentMenuId":"17"},
{"menuId":"176","menuName":"风险控制日志","menuIcon":"fa-list","menuHref":"views/logs/index.html","parentMenuId":"17"},
{"menuId":"18","menuName":"数据分析","menuIcon":"fa-bar-chart","menuHref":"","parentMenuId":"1"},
{"menuId":"184","menuName":"总体概览","menuIcon":"fa-list","menuHref":"views/analysis/index.html","parentMenuId":"18"},
{"menuId":"181","menuName":"日度分析","menuIcon":"fa-list","menuHref":"views/analysis/dayanalysis.html","parentMenuId":"18"},
{"menuId":"182","menuName":"月度分析","menuIcon":"fa-list","menuHref":"views/analysis/monthanalysis.html","parentMenuId":"18"},
{"menuId":"183","menuName":"年度分析","menuIcon":"fa-list","menuHref":"views/analysis/yearanalysis.html","parentMenuId":"18"},
{"menuId":"19","menuName":"风险控制","menuIcon":"fa-shield","menuHref":"","parentMenuId":"1"},
{"menuId":"191","menuName":"终端风险","menuIcon":"fa-list","menuHref":"views/datagrid/index.html","parentMenuId":"19"},
{"menuId":"192","menuName":"分销商风险","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"19"},
{"menuId":"193","menuName":"账户风险","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"19"},
{"menuId":"194","menuName":"风险预警规则","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"19"},
{"menuId":"20","menuName":"系统管理","menuIcon":"fa-cog","menuHref":"","parentMenuId":"1"},
{"menuId":"201","menuName":"界面管理","menuIcon":"fa-list","menuHref":"views/datagrid/index.html","parentMenuId":"20"},
{"menuId":"202","menuName":"访问控制","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"20"},
{"menuId":"203","menuName":"系统公告","menuIcon":"fa-list","menuHref":"views/datagrid2/index.html","parentMenuId":"20"}
]
}
}
文档信息
- 本文作者:itlgl
- 本文链接:https://itlgl.com/note/2019/03/13/issues-35/
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)