有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring使用文件中的参数访问属性。Java类中的属性

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

我得到了一个包含如下属性的属性:

message=Hello {0}, welcome.

我希望在Java类中使用字符串访问此属性,并在该类中设置参数

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

你知道怎么做吗


共 (1) 个答案

  1. # 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; 
    }