- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有将近一百个这样的属性
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 .*.order.[a-z]*=)
有人知道 spring 或 JDK 是否为此提供了一些东西吗?我想我将不得不获取 ResourceBundle 并获取所有 key 并对它们进行正则表达式...
最佳答案
我不认为你可以在 Spring 中做到这一点,但这里有一些代码可能会有所帮助:
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,但我相信您可以将它整理成更有用的东西。
关于java - 在 spring 中从 ResourceBundleMessageSource 按模式获取属性键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4240537/
如果可以通过 Spring 自动解析嵌套属性,我需要帮助。我正在使用 Spring 4.2.4、Java 8 并且属性文件看起来像- # messages.properties company.nam
我有一个 JSP-Tagfile,它呈现 html-header 并定义我的 Javascript/Stylesheet 资源。 "/> "> ... 现在我不想为 java
我正在尝试以编程方式配置 Spring MVC 而不是 xml 文件。几乎一切都工作正常,但我在 ResourceBundleMessageSource 声明方面遇到了麻烦。 我的配置类如下所示: @
我目前正在使用以下代码,该代码运行正常: /WEB-INF/resources/lang/lang
消息包: 这里是名为 messages.propertie 的文件 greeting=hi! 然后使用消息标签: 属性标签中的值(即“message”)应该与我的属性文件名(即“mess
我正在尝试设置一些消息捆绑。我有 2 个文件: message.properties message_en_US.properties bean 类: 实现: msgSrc.getM
我在 /resources 中有两个语言文件: mymessage_de.properties mymessage_en.properties 我想创建一个消息包,如果无法解析包,则默认情况下应回退到
有人可以向我解释 PropertyPlaceholderConfigurer 和 ResourceBundleMessageSource 之间的区别吗?什么时候用第一个,什么时候用后者? 最佳答案 P
我配置了 appstrings.properties 文件,其中包含与我的应用程序使用的主题相关的字符串。我的应用程序有两个主题。两者都有自己的 appstrings.properties 文件,位于
我正在学习 Spring 。我尝试使用 ResourceBundleMessageSource,这是我尝试过的示例。 主应用 public class MainApp { public sta
您好,我正在尝试在我的 Spring MVC Web 应用程序中配置消息源。 我目前已经让它与 ReloadableResourceBundleMessageSource 一起运行,但我无法让它与 R
我不明白为什么我必须创建bean ResourceBundleMessageSource, public class AppConfig { @Bean public ResourceBundleMe
我想模仿 Grails 的方式来解析 i18n 消息。 在 WEB-INF/i18n/我有以下目录: admin/messages_EN.properties admin/messages_FR.pr
我有将近一百个这样的属性 NotEmpty.order.languageFrom=Field Language can't be empty NotEmpty.order.langua
我定义了以下 Spring bean: classpath:messages Controller : @Contr
所以帮助回答这个问题Thymeleaf + Spring (not Boot) - how to show messages from messageSource 我发现如果您使用Reloadable
我正在尝试从 jar 文件加载 message.properties 文件,但它没有找到该文件(我已经尝试了几个小时)。 messages/mes
我在 Spring MVC 文件的 xml 文件中定义了一个 ResourceBundleMessageSource bean。定义如下: 但是Intellij将defaultEnco
我是一名优秀的程序员,十分优秀!