我正在寻找是否可以根据从托管 bean 接收的值获取资源包值的可能性。它在数据表、数据网格以及呈现值的其他组件中可能很有用。
我试过这段代码:
<h:outputText value="#{resourceBundle['myBean.myMsg']}" />
但它没有用。我的 outputText 无法从资源包中获取值。结果是这样的:
???myBean.myMsg
我正在寻找是否可以根据从托管 bean 接收的值获取资源包值的可能性。它在数据表、数据网格以及呈现值的其他组件中可能很有用。
我试过这段代码:
<h:outputText value="#{resourceBundle['myBean.myMsg']}" />
但它没有用。我的 outputText 无法从资源包中获取值。结果是这样的:
???myBean.myMsg
如果你得到这意味着它在你的资源文件中???myBean.myMsg
找不到myBean.myMsg字符串......
我猜您想使用 myBean.myMsg 中的密钥(而不是字符串myBean.myMsg)?
在这种情况下,只需删除''
它周围的
<h:outputText value="#{resourceBundle[myBean.myMsg]}" />
否则它将被用作字符串而不是 EL 表达式
如果您想在所有视图中访问它,您需要在 中声明您的包,例如:faces-config.xml
<application>
<resource-bundle>
<base-name>path-to-your-resource-bundle</base-name>
<var>bundle</var>
</resource-bundle>
</application>
这样它就可以在视图中访问
<h:outputText value="#{bundle['myBean.myMessage']}" />
或直接将其加载到您的视图中
<f:loadBundle basename="path-to-your-resource-bundle" var="bundle" />
<body>
<h:outputText value="#{bundle['myBean.myMessage']}" />
</body>
在任何情况下,您的资源包都必须包含字符串,其中包含消息的名称和值对。
myBean.myMessage = This is my message
另外值得注意的是,资源包应该放在src/main/resources
项目的文件夹中。因此,bundle.properties
在上述文件夹中将base-name
有bundle
.
关于用法:
<h:outputText value="#{bundle['myBean.myMessage']}" />
使用托管 bean 的属性,该属性从包中评估为您想要的字符串:<h:outputText value="#{bundle[myBean.myMessage]}" />
with
@ManagedBean
@...Scoped
public class MyBean {
private String myMeggase = "bundle.string";//getter + setter
}
将值存储在 中<ui:param>
,这在模板中可能很有用:
<ui:param name="bndl" value="#{myBean.myMessage}"/>
<h:outputText value="#{bundle[bndl]}" />
使用相同的托管 bean。