简体   繁体   English

如何基于从托管Bean接收的值获取资源束值?

[英]How to get resource bundle value based on values received from managed bean?

I am looking for the possibility if I can get resourcbundle values based on values received from managed bean. 我正在寻找是否有可能基于从托管bean接收到的值获得resourcundle值的可能性。 It may be useful in datatable, datagrid, and also with other components where values are rendered. 它在数据表,数据网格以及其他呈现值的组件中可能很有用。

I tried with this code: 我尝试使用以下代码:

<h:outputText value="#{resourceBundle['myBean.myMsg']}" />

But it didnt work. 但是它没有用。 My outputText was not able to fetch the value from resourcebundle. 我的outputText无法从resourcebundle获取值。 Result was like: 结果就像:

???myBean.myMsg

If you getting ???myBean.myMsg that means that it could not find myBean.myMsg string in your resource file... 如果获取???myBean.myMsg ,则意味着它无法在资源文件中找到myBean.myMsg字符串...

I guess you want to use the key inside the myBean.myMsg (and not the string myBean.myMsg )? 我猜您想使用myBean.myMsg(而不是字符串myBean.myMsg )内的键?

In that case just remove the '' that surrounds it 在这种情况下,只需将其周围的''

<h:outputText value="#{resourceBundle[myBean.myMsg]}" />

Otherwise it will be used as a string and not as EL expression 否则它将用作字符串而不是EL表达式

You need to either declare your bundle in faces-config.xml , if you want to have access to it in all views, like: 如果要在所有视图中访问它,则需要faces-config.xml声明您的包 ,例如:

<application>
    <resource-bundle>
        <base-name>path-to-your-resource-bundle</base-name>
        <var>bundle</var>
    </resource-bundle>
</application>

so that it is accessible in view with 以便可以通过以下方式访问它

<h:outputText value="#{bundle['myBean.myMessage']}" />

or load it directly in your view with 直接将其加载到您的视图

<f:loadBundle basename="path-to-your-resource-bundle" var="bundle" />
<body>
    <h:outputText value="#{bundle['myBean.myMessage']}" />
</body>  

In any case, your resource bundle has to contain the string, which contains name and value pair for your message. 无论如何,您的资源包都必须包含字符串,该字符串包含消息的名称和值对。

myBean.myMessage = This is my message

Also worth noting that resource bundles should be placed in src/main/resources folder of your project. 同样值得注意的是,资源束应放在项目的src/main/resources文件夹中。 So, bundle.properties in the abovementioned folder will have base-name of bundle . 因此,上述文件夹中的bundle.properties将具有bundle base-name

Regarding the usage: 关于用法:

  • Use string from message bundle itself: <h:outputText value="#{bundle['myBean.myMessage']}" /> 使用消息包本身中的字符串: <h:outputText value="#{bundle['myBean.myMessage']}" />
  • Use managed bean's property that evaluates to your desired string from the bundle: <h:outputText value="#{bundle[myBean.myMessage]}" /> with 使用托管bean的属性,该属性的结果为从捆绑软件中获取所需的字符串: <h:outputText value="#{bundle[myBean.myMessage]}" />

     @ManagedBean @...Scoped public class MyBean { private String myMeggase = "bundle.string";//getter + setter } 
  • Store value in <ui:param> , which might be useful in templating: 将值存储在<ui:param> ,这可能对模板化很有用:

     <ui:param name="bndl" value="#{myBean.myMessage}"/> <h:outputText value="#{bundle[bndl]}" /> 

    with the same managed bean. 使用相同的托管bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM