加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

配置文件中,字符串占位符替换

发布时间:2020-12-14 05:35:54 所属栏目:百科 来源:网络整理
导读:/** * @description 动态list获取messages.properties中的校验信息 * @author chenhui * @created_at 2019年8月13日 下午5:23:00 * @param key map * @return */public static void throwMessage(String key,ListString list) {String value = getMessage(ke
      /**
	 * @description 动态list获取messages.properties中的校验信息
	 * @author chenhui
	 * @created_at 2019年8月13日 下午5:23:00
	 * @param key map
	 * @return
	 */
	public static void throwMessage(String key,List<String> list) {
		String value = getMessage(key);//getMessage方法是根据properties文件中的key 获得value
		for(int i = 0; i<list.size();i++) {
			value = value.replaceAll("{"+i+"}",list.get(i));
		}
		CstValidationException.newAndThrow(value);
	}
	
	/**
	 * @description 动态map获取messages.properties中的校验信息
	 * @author chenhui
	 * @created_at 2019年8月13日 下午5:23:00
	 * @param key
	 * @return
	 */
	public static void throwMessage(String key,Map<String,String> map) {
		String[] value =new String[1];//getMessage方法是根据properties文件中的key 获得value
		value[0] = getMessage(key);
		map.forEach((k,v)->{
			if(value[0].contains(k)) {
				value[0] = value[0].replaceAll("{"+k+"}",v);
			}
		});
		CstValidationException.newAndThrow(value[0]);
	}
//配置文件的写法:
key=这是{k}值
key=这{1}是{2}值

  思路:从message的properties文件中通过key取出value,将value中带有占位符得String字符串用正则表达式精确匹配,替换掉

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读