2013-03-06 25 views
2

我在尋找可能性,如果我可以根據從託管bean收到的值獲取resourcbundle值。它可能對數據表,數據網格以及其他渲染值的組件有用。如何根據從託管bean收到的值獲取資源包值?

我試着用這個代碼:

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

但它沒有工作。我的outputText無法從resourcebundle中獲取值。結果是這樣的:

???myBean.myMsg 

回答

5

如果你得到???myBean.myMsg這意味着它無法找到myBean.myMsg字符串中的資源文件...

我猜你想使用的myBean.myMsg內鍵(不是字符串myBean.myMsg)?

在這種情況下,只是刪除它周圍

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

''否則將被用來作爲一個字符串,而不是作爲EL表達式

+0

感謝您的快速回復。刪除後,'它的工作。 :) – 2013-03-06 07:43:04

+1

+1不關注OP的意圖:) – skuntsel 2013-03-06 08:21:25

1

你需要或者聲明你的包在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-namebundle

關於用法:從消息包本身

  • 使用字符串:<h:outputText value="#{bundle['myBean.myMessage']}" />
  • 使用管理bean的屬性,其值是你想要的字符串從包:<h:outputText value="#{bundle[myBean.myMessage]}" />

    @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。

相關問題