在Spring中从ResourceBundleMessageSource获取属性键 [英] Get property keys by pattern from ResourceBundleMessageSource in spring
问题描述
我有近百个这样的属性
NotEmpty.order.languageFrom=Field Language can't be empty
NotEmpty.order.languageTo=Field Language can't be empty
NotEmpty.order.description=Description field can't be empty
NotEmpty.order.formType=FormType field can't be empty
NotEmpty.cart.formType=FormType field can't be empty
NotEmpty.cart.formType=FormType field can't be empty
我希望能够在没有密钥知识的情况下获得这些属性(两个键/值)......类似于 getPropertyPair(regexp。*。订单。[az] * =)
And I'd like to be able getting these properties (both keys/values) without previous knowledge of keys ...something like getPropertyPair(regexp .*.order.[a-z]*=)
有人知道spring或JDK是否提供了相应的东西吗?我想我必须得到ResourceBundle并获取所有密钥并正则表达它们...
Does anybody know if spring or JDK offers something for that ? I suppose I'm gonna have to get the ResourceBundle and get all the keys and regexp them...
推荐答案
我不知道我认为你可以在Spring中做到这一点,但这里有一些可能有用的代码:
I don't think you can do it in Spring, but here's some code that might help:
public class Main {
public static void main(String[] args) {
ResourceBundle labels = ResourceBundle.getBundle("spring-regex/regex-resources", Locale.UK);
Enumeration<String> labelKeys = labels.getKeys();
// Build up a buffer of label keys
StringBuffer sb = new StringBuffer();
while (labelKeys.hasMoreElements()) {
String key = labelKeys.nextElement();
sb.append(key + "|");
}
// Choose the pattern for matching
Pattern pattern = Pattern.compile(".*.order.[a-z]*\\|");
Matcher matcher = pattern.matcher(sb);
// Attempt to find all matching keys
List<String> matchingLabelKeys = new ArrayList<String>();
while (matcher.find()) {
String key=matcher.group();
matchingLabelKeys.add(key.substring(0,key.length()-1));
}
// Show results
for (String value: matchingLabelKeys) {
System.out.format("Key=%s Resource=%s",value,labels.getString(value));
}
}
}
有点hacky但我相信你可以将它整理成更有用的东西。
It's a bit hacky but I'm sure you can tidy it up into something more useful.
这篇关于在Spring中从ResourceBundleMessageSource获取属性键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!