属性占位符Spring等效于JSF2

时间:2012-11-15 22:32:33

标签: jsf-2

我有一个高度可配置的JSf2 Web应用程序。我需要能够将位于配置文件中的属性(类路径中的config.properties)传递给使用XML配置声明的jsf 2.0 managed beabs。 这是一个例子:

<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正确创建bean。我知道我可以在春天使用property-placeholder做到这一点。

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

有没有办法在JSF 2.0中执行此操作?有没有其他解决方案可以做我想做的事情?

请帮忙!! 。

1 个答案:

答案 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");
    
相关问题