2012-11-15 47 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