使用 faces-config.xml 初始化 JSF bean

发布于 2024-11-29 17:26:25 字数 1104 浏览 5 评论 0原文

我有一个名为 Bucket 的 Bean,它有一个 HashMap。我想初始化 bean 并使用属性文件填充 faces-config.xml 中的 HashMap。我怎样才能做到这一点?

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>

该映射必须以bucket.id 作为键,以DL_SERVICE 作为值。

感谢高级~

I've a Bean named as Bucket, it has a HashMap. I want to initialize the bean and polulate the HashMap in the faces-config.xml with a property file. How can I do that?

Bean:

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

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

Property file, named as bundle.properties, and it's on the classpath.

bucket.id=DL_SERVICE

faces-config.xml file :

<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>

That Map has to have the bucket.id as the key and DL_SERVICE as the value.

Thanks in Advanced~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2024-12-06 17:26:25

假设属性文件与 BundleBean 位于相同的 ClassLoader 上下文中,请调用如下方法:

@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 setter 中调用它,或者在 bundlePropertiesMap getter 中执行惰性检查。

Assuming the properties file is in the same ClassLoader context as BundleBean, call a method like this:

@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();
    }
}

This is best invoked using the @PostConstruct annotation. If that is not an option, call it either in the bundleFileName setter or perform a lazy check in the bundlePropertiesMap getter.

記憶穿過時間隧道 2024-12-06 17:26:25

您可以使用具有更高级依赖注入机制的 spring 来做到这一点。
当你将 spring 与 jsf 集成时,你可以在 spring 上下文中定义你的 jsf bundlebean

<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>

you can do that with spring that has a more advanced dependency injection mechanism.
when you integrate spring with jsf, you can define your jsf bundlebean in the spring context

<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>
苏别ゝ 2024-12-06 17:26:25

如果我没记错的话,JSF 在初始化 bean 时会调用相应的 setter。因此,提供一个方法 public void setBundleFileName(String filename) 应该可以工作。

If I'm not mistaken, JSF calls the corresponding setters when initializing the bean. Thus providing a method public void setBundleFileName(String filename) should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文