【问题标题】:How to get a localized message from enum displayed如何从显示的枚举中获取本地化消息
【发布时间】:2012-11-28 13:27:02
【问题描述】:

例如我有一个

enum State { OK, WARN, ERROR } 

还有一个message.properties,带有以下键:

my.state.OK=Ok
my.state.WARN=Warning
my.state.ERROR=Error

给定一个具有State 类型属性的bean,例如bean.state,我想显示属性状态的文本。

类似:

#{text['my.state.' + bean.state]}

这似乎是不可能的,因为 + 运算符不适用于字符串。

有什么解决办法吗?

【问题讨论】:

  • 没关系,我明白了:<ui:param name="msgKey" value="my.state.#{bean.state}" />#{text[msgKey]}

标签: jsf-2 properties localization enums el


【解决方案1】:

向表示标签键的枚举添加一个额外的属性。

public enum State {

    OK, WARN, ERROR;

    private String labelKey;

    private State() {
        this.labelKey = "my.state." + name();
    }

    public String getLabelKey() {
        return labelKey;
    }

}

这样你就可以参考如下:

#{text[bean.state.labelKey]}

这样你就不需要在所有地方重复<ui:param name="msgKey" value="my.state.#{bean.state}" />

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    相关资源
    最近更新 更多