简体   繁体   English

从Java类中的file.properties访问带有参数的属性

[英]Accessing attribute with argument from file.properties in Java class

Let me explain what I want to do: 让我解释一下我想做什么:

I got a properties containing a property like this: 我有一个包含这样的属性的属性:

message=Hello {0}, welcome.

I'd like to access this property in a Java class using a String and set the parameter in that class. 我想使用String在Java类中访问此属性,然后在该类中设置参数。

I've already use fmt:message and fmt:param to display this kind of property in a JSP but I want to manipulate it in a Java object now (I already know how to inject a property into the class). 我已经使用fmt:message和fmt:param在JSP中显示这种属性,但是我现在想在Java对象中对其进行操作(我已经知道如何将属性注入到类中)。

Any idea on how to do this? 关于如何执行此操作的任何想法?

You can use java.util.ResourceBundle and java.text.MessageFormat Some examples 您可以使用java.util.ResourceBundlejava.text.MessageFormat的一些示例

private String getString( String bundle, String key, String defaultValue, Object... arguments ){
    String result = ResourceBundle.getBundle( bundle ).getString( key );
    if ( result == null ){
        result = defaultValue;
    }
    if ( arguments.length > 0 && result != null ){
        result = MessageFormat.format( result, arguments );
    }
    return result; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM