更改之前在 faces-config.xml 中初始化的 beans
public class MyBean {
private Integer [] myField;
public Integer [] getMyField() {
return myField;
}
public void setMyField(Integer [] myField) {
this.myField = myField;
}
我以这种方式在 faces-config.xml 中初始化同一个 bean
<managed-bean-name>myBean</managed-bean-name>
<managed-bean-class>com.path.bean.MyBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>myField</property-name>
<list-entries>
<value>6</value>
<value>12</value>
<value>24</value>
</list-entries>
</managed-property>
</managed-bean>
然后,在应用程序中我想更改这些值。要做到这一点:
MyBean myBean = new MyBean();
Integer [] results = myBean.getMyfield();
//Change the value of this array
visualizationBean.setResultsPerPage(results);
但这是不可能的,Integer [] results = myBean.getMyfield()
给了我一个null
。不管怎样,在我的应用程序的界面中,我可以看到 bean 已正确初始化,因为它保存了值 6、12 和 24。
有什么帮助吗? 提前致谢
public class MyBean {
private Integer [] myField;
public Integer [] getMyField() {
return myField;
}
public void setMyField(Integer [] myField) {
this.myField = myField;
}
And I initialize this same bean in faces-config.xml in this way
<managed-bean-name>myBean</managed-bean-name>
<managed-bean-class>com.path.bean.MyBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>myField</property-name>
<list-entries>
<value>6</value>
<value>12</value>
<value>24</value>
</list-entries>
</managed-property>
</managed-bean>
Then, in the application I want to change these values. To do it:
MyBean myBean = new MyBean();
Integer [] results = myBean.getMyfield();
//Change the value of this array
visualizationBean.setResultsPerPage(results);
But this is not possible, Integer [] results = myBean.getMyfield()
gives me a null
. Anyway, in the interface of my application, I can see that the bean is correctly initialize, because it holds the values 6, 12 and 24.
Any kind of help??
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
new
实例化MyBean
时,它不会查找 Faces-Config。它只会使用构造函数创建一个对象。如果您使用
jsf2.0
,则在初始化上下文时使用 Bean 进行初始化,并从作用域映射中检索
Bean
实例。如果它的应用范围是bean的话。更新:
你的托管bean应该看起来像这样,
如果你只想在另一个bean中使用你的bean,那么正如BalusC建议的那样,只需注入它并获取填充值,而不使用
new
例如:如果您希望将
MyBean
填充到SomeOtherBean
中,然后为 jsf 1.2 Update
,没有注释,您需要配置您的
faces-config.xml
如下所示As you instantiate
MyBean
usingnew
, it won't look for the Faces-Config. and it will simply create an object using constructor.If you are using
jsf2.0
make bean to initialize when your context is being initialized usingand retrieve the
Bean
instance from scoped map. if its application scoped bean the.Update:
your managed bean should look like ,
if you just want to use your bean in another bean then as BalusC suggested simply inject it and get the filled value , without using
new
for example: if you want your
MyBean
poppulated inSomeOtherBean
thenUpdate
for jsf 1.2 , there is no annotations, you need to configure your
faces-config.xml
as shown below您正在使用 new 创建对象,如
MyBean myBean = new MyBean();
所以您肯定会得到 null。
You are creating object using new like
MyBean myBean = new MyBean();
So you are sure to get null.