【问题标题】:Accessing attribute with argument from file.properties in Java class使用 Java 类中 file.properties 的参数访问属性
【发布时间】:2012-03-08 10:16:11
【问题描述】:

让我解释一下我想做什么:

我得到一个包含这样一个属性的属性:

message=Hello {0}, welcome.

我想使用 String 访问 Java 类中的此属性并在该类中设置参数。

我已经使用 fmt:message 和 fmt:param 在 JSP 中显示这种属性,但我现在想在 Java 对象中操作它(我已经知道如何将属性注入到类中)。

你知道怎么做吗?

【问题讨论】:

    标签: java spring spring-mvc properties-file


    【解决方案1】:

    您可以使用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; 
    }
    

    【讨论】:

    • 更多细节可能吗?我已经在使用 ResourceBundleMessageSource 来访问 .jsp 文件中的属性并将它们与 fmt 一起使用。我真的很想知道如何访问该属性并在Java中设置参数。
    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 2017-06-12
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 2023-03-18
    相关资源
    最近更新 更多