2012-11-15 44 views
1

我有一個高度可配置的JSf2 Web應用程序。我需要能夠將位於配置文件(類路徑中的config.properties)中的屬性傳遞給使用XML配置聲明的jsf 2.0託管的beabs。 下面是一個例子:屬性佔位符JSF2的Spring等價物

<managed-bean> 
    <managed-bean-name>myBean</managed-bean-name> 
    <managed-bean-class>${config.myBean.class}</managed-bean-class> 
    <managed-bean-scope>application</managed-bean-scope> 
    <managed-property> 
     <property-name>property1</property-name> 
     <value>#{config.myBean.property1}</value> 
    </managed-property> 
      <managed-property> 
     <property-name>property2</property-name> 
     <value>#{config.myBean.property2}</value> 
    </managed-property> 
</managed-bean> 

我的問題是我怎麼可以從config.properties訪問這些價值觀,並有JSF創建的豆正常。我知道我可以在春季使用屬性佔位符來做到這一點。

<context:property-placeholder location="classpath:/config/config.properties" /> 

有沒有辦法在JSF 2.0中做到這一點?有沒有另一種解決方案去做我想做的事情?

請幫忙!! 。

回答

1

JSF提供了一個非常健壯的屬性文件管理機制,它比Spring提供的設置稍微多一些。加載一個屬性文件(或資源束,因爲它是所謂的內JSF)

  1. 定義<resource-bundle/><application/>元件下,在faces-config.xml

    <application> 
        <resource-bundle> 
         <base-name> 
          com.me.resources.messages 
         </base-name> 
         <var> 
          msg 
         </var> 
        </resource-bundle> 
    </application> 
    

    <base-name>指包/目錄結構中你有你的屬性文件和<var>引用你將用來訪問應用程序中的文件屬性的引用變量。確保您可以在Web應用程序內訪問該目錄結構。我的建議是將其存儲在一個普通的Java包中,並與您的應用捆綁在一起。

  2. 現在,您可以在託管bean中引用你的財產

    <h:outputText value="#{msg['messages.error']}" /> 
    

    ,並在您管理的Bean

    ResourceBundle securityBundle = ResourceBundle.getBundle("com.me.resources.messages"); 
    String errorMessage = securityBundle.getString("messages.error"); 
    
+0

@ kolossus.That的我現在有什麼,但它不工作。因爲'msg'在創建bean時不可用。我真正需要的是在創建託管bean期間訪問這些屬性。任何想法 ???謝謝。 – alpha2011

+0

@ alpha2011你可以試試'@ManagedProperty(value =「#{msg ['messages.error']}」)'選項。當然,它不像你從春天得到的那樣具有說明性,但它仍然是乾淨的 – kolossus