2011-08-15 13 views
0

私はBucketという名前のBeanを持っていますが、これにはHashMapがあります。 Beanを初期化し、faces-config.xml内のHashMapをプロパティファイルでポーリングします。どうやってやるの?faces-config.xmlで初期化するJSF Bean

豆:

public class BundleBean { 
private Map<String, String> bundlePropertiesMap = new HashMap<String, String>(); 
private String bundleFileName; 

// Setter, getter goes here.... 
} 

bundle.propertiesという名前のプロパティファイル、およびそれがクラスパス上にあります。

bucket.id=DL_SERVICE 

のfaces-config.xmlのファイル:

<managed-bean> 
    <description> 
     Java bean class which have bundle properties. 
    </description> 
    <managed-bean-name>bundleBean</managed-bean-name> 
    <managed-bean-class>org.example.view.bean.BundleBean</managed-bean-class> 
    <managed-bean-scope>application</managed-bean-scope> 
    <managed-property> 
     <property-name>bundleFileName</property-name> 
     <value>bundle.properties</value> 
    </managed-property> 
</managed-bean> 

地図値としてキーとDL_SERVICEとしてbucket.idを持っている必要があります。 Beanを初期化するとき、私は間違っていない場合はアドバンスト〜

答えて

2

BundleBeanと同じクラスローダのコンテキストにある春のコンテキストであなたのJSF bundlebeanを定義することができます。

@SuppressWarnings("unchecked") 
private void loadBundle(String bundleFileName, Map<String, String> map) 
                 throws IOException { 
    InputStream in = BundleBean.class.getResourceAsStream(bundleFileName); 
    try { 
     Properties props = new Properties(); 
     props.load(in); 
     ((Map) map).putAll(props); 
    } finally { 
     in.close(); 
    } 
} 

@PostConstructアノテーションを使用するのが最も適しています。それがオプションでない場合は、bundleFileNameセッターで呼び出すか、bundlePropertiesMapゲッターで遅延チェックを実行してください。

0

おかげで、JSFは、対応するセッターを呼び出します。したがって、メソッドpublic void setBundleFileName(String filename)を提供する必要があります。

+0

私の問題は、bundleFileNameプロパティではなく、HashMapプロパティを設定することです。 –

+0

@Kugathasan妥当な時点(セッターなどの最初のアクセス)で手動でファイルをロードできます。 'Properties'クラスを使ってみてください。 – Thomas

1

これは、先進的な依存性注入機構を持つスプリングで行うことができます。 あなたはJSFで春を統合するとき、あなたはこのようなメソッドを呼び出し、プロパティファイルを想定し

<bean id="injectCollection" class="CollectionInjection"> 
     <property name="map"> 
      <map> 
       <entry key="someValue"> 
        <value>Hello World!</value> 
       </entry> 
       <entry key="someBean"> 
        <ref local="oracle"/> 
       </entry> 
      </map> 
     </property> 
+0

しかし、私の要件は、XMLファイルからではなく、プロパティファイルから値を取得することです。 –

関連する問題