0

Bucket という名前の Bean があり、HashMap があります。Bean を初期化し、faces-config.xml の HashMap にプロパティ ファイルを設定したいと考えています。どうやってやるの?

豆:

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>

そのマップには、キーとしてのbucket.idと値としてのDL_SERVICEが必要です。

ありがとうございます〜

4

3 に答える 3

2

プロパティファイルがと同じClassLoaderコンテキストにあると仮定して、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

于 2011-08-15T09:45:48.897 に答える
0

私が間違っていなければ、JSF は Bean の初期化時に対応するセッターを呼び出します。したがって、メソッドを提供するpublic void setBundleFileName(String filename)とうまくいくはずです。

于 2011-08-15T07:48:10.397 に答える