使用 faces-config.xml 初始化 JSF bean
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设属性文件与
BundleBean
位于相同的 ClassLoader 上下文中,请调用如下方法:最好使用
@PostConstruct
注释来调用。如果这不是一个选项,请在bundleFileName
setter 中调用它,或者在bundlePropertiesMap
getter 中执行惰性检查。Assuming the properties file is in the same ClassLoader context as
BundleBean
, call a method like this:This is best invoked using the
@PostConstruct
annotation. If that is not an option, call it either in thebundleFileName
setter or perform a lazy check in thebundlePropertiesMap
getter.您可以使用具有更高级依赖注入机制的 spring 来做到这一点。
当你将 spring 与 jsf 集成时,你可以在 spring 上下文中定义你的 jsf bundlebean
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
如果我没记错的话,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.