在JSF表单之间传递值。我无法让它发挥作用

时间:2017-11-17 19:03:44

标签: jsf web jsf-2.2

我阅读了有关此主题的其他帖子,但我仍然无法让它工作

这是我的豆子:

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”有时为空

1 个答案:

答案 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";
    }
}