我阅读了有关此主题的其他帖子,但我仍然无法让它工作
这是我的豆子:
Bean1:
@ManagedBean()
@SessionScoped
public class Bean1 implements Serializable {
//Here are some important Properties
public String buttonPressed() {
return "bean2.xhtml";
}
}
<h:form>
<p:commandButton action="#{Bean1.buttonPressed}" value="Do Work"/>
</h:form>
Bean2:
@ManagedBean()
@SessionScoped
public class Bean2 implements Serializable {
@ManagedProperty(value = "#{Bean1}")
private Bean1 b1;
//getter/setter is here
public String doWorkOnSubmit() {
//Access important Properties from bean1
b1.getFoo()
}
}
现在我有两个问题
1。)如果按下Bean1中的按钮,如何调用“doWorkOnSubmit”?我不能使用构造函数,因为它是SessionScoped而我不知道如何调用doWorkOnSubmit un submit
2.)托管属性“b1”有时为空
答案 0 :(得分:0)
点击Do Work
按钮即表示您正在调用Bean1.buttonPressed()
操作方法,您可以通过在Bean2
中注入doWorkOnSubmit()
来致电Bean2
s Bean1
以下是您的代码更改:
@ManagedBean()
@SessionScoped
public class Bean1 implements Serializable {
//Here are some important Properties
/*
* Inject Bean2 here. since both beans are session scoped,
* there ain't gonna be any problem in injection.
*/
@ManagedProperty(value = "#{Bean2}")
private Bean2 b2;
//GETTER SETTER for b2
public String buttonPressed() {
//Here you will be invoking injected managed beans method.
b2.doWorkOnSubmit();
return "bean2.xhtml";
}
}