我有一个
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]}
这似乎不可能只因为+
运算符不能用于字符串。
任何解决方法?
答案 0 :(得分: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}" />
。