java 在托管 bean 之间传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17587917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:24:16  来源:igfitidea点击:

Passing parameters between managed beans

javajsfjakarta-eejavabeans

提问by yrazlik

I am trying to pass some parameters from one managed bean to another. I saw similar question and applied their solutions but does not work. Here is the code:

我试图将一些参数从一个托管 bean 传递到另一个。我看到了类似的问题并应用了他们的解决方案,但不起作用。这是代码:

In my moneytransfer.xhtml file:

在我的 moneytransfer.xhtml 文件中:

<h:commandButton action="#{moneyTransferBean.transferAccounts()}" value="Continue">
   <f:param name="sender" value="#{extTableSelectionBean.sender}" />
</h:commandButton>

My extTableSelectionBean:

我的 extTableSelectionBean:

@ManagedBean
@ViewScoped
public class ExtTableSelectionBean implements Serializable {
    private Account sender;

    public void setSender(Account sender){
        this.sender=sender;
    }


    public Account getSender(){
        return sender;
    }

and the moneyTransferBean:

和 moneyTransferBean:

@ManagedBean
@ViewScoped
public class MoneyTransferBean {
     @ManagedProperty("#{extTableSelectionBean .sender}")
     private Account sender;
     //NO SETTER-GETTER FOR sender here

     public void transferAccounts() throws IOException {        

         if (sender != null)
         {  
             FacesContext.getCurrentInstance().getExternalContext().redirect("transferaccount.xhtml");
         }
     }
}

I see that in extTableSelectionBean, the "sender" is succesfully set. The problem is, when i get to the moneyTransferBean, sender becomes null. What should i do about it, what am i doing wrong?

我看到在 extTableSelectionBean 中,“发件人”已成功设置。问题是,当我到达 moneyTransferBean 时,发件人变为空。我该怎么办,我做错了什么?

Thanks

谢谢

回答by erencan

Two things are mixed here. Injecting a bean into another bean and adding a parameter to commandButton.

两件事在这里混杂在一起。将一个 bean 注入另一个 bean 并向commandButton.

Account senderis tried to inject to MoneyTransferBean, however there is no action will be performed since there is no getter setter, so injection will be failed.

Account sender尝试注入到 MoneyTransferBean,但是由于没有getter setter,因此不会执行任何操作,因此注入将失败。

senderis tried to set through a commandButton to send as parameter, but there is no implementation for it. @ManagedProperty annotation should be changed for reading the parameter from command button. I assume that sender is set in any place before submitting of commandButton

sender试图通过 commandButton 设置作为参数发送,但没有实现。应该更改@ManagedProperty 注释以从命令按钮读取参数。我假设在提交之前在任何地方设置了发件人commandButton

@ManagedProperty(value="#{param.sender}")
private Account sender;

There are further methods to send or set data in managed beans. Please read the BalusC answer.

还有其他方法可以在托管 bean 中发送或设置数据。请阅读 BalusC 的答案。

Related Post

相关帖子

https://stackoverflow.com/a/4994833/892994

https://stackoverflow.com/a/4994833/892994