【问题标题】:Calling one bean from another bean and setting the value to the bean and display from another bean从另一个 bean 调用一个 bean 并将值设置为 bean 并从另一个 bean 显示
【发布时间】:2015-02-24 10:24:45
【问题描述】:

我有一个由托管 bean @RequestScoped 'A' 支持的对话框。我正在从另一个 bean 'B'@RequestScoped 调用对话框。所以我使用@ManagedProperty'B' 调用'A'。我已经设置了来自'B''A' 的值(对象、变量等)用于显示,这将取决于'B' 的对象。所有值都设置正确,但是当对话框打开时,我为'A' 设置的值不会显示。

如何实现这个目标??

我的意思是调用一个由一个 bean 支持的对话框并从另一个 bean 设置支持的 bean 的值??

我使用了p:dataTable 行上的按钮:

<p:column headerText="Actions">
<p:commandButton icon="ui-icon-search" title="View"
process="@this"
oncomplete="receiptViewWidget.show()"
action="#{receiptRepoMB.forReceiptDialog}">
<f:setPropertyActionListener
target="#{receiptRepoMB.receiptDetObj}" value="#{rd}" />
</p:commandButton>
</p:column>

显示对话框但不显示值。

'B' 的代码 sn-p:

@ManagedProperty(value = "#{receiptMB}")
private ReceiptMB receiptMB;
public ReceiptMB getReceiptMB() {
return receiptMB;
}
public void setReceiptMB(ReceiptMB receiptMB) {
this.receiptMB = receiptMB;
}
public void forReceiptDialog(){
ReceiptModel receiptObj = receiptDetObj.getReceiptModel();
receiptMB.setReceiptSummary(receiptObj);
}

【问题讨论】:

  • 不要忘记为每个属性添加获取和设置。发布您的 java 代码。
  • 好的,我正在发布代码。

标签: jsf primefaces managed-bean


【解决方案1】:

这里的问题是当你尝试访问它时,从 B 使用的 A 的值将会丢失。

我建议您定义具有更长范围的 A,以便在 B 调用时保持其值。(应该可以使用 @ViewScoped 之类的东西)。

@ViewScoped
public class A {}

@RequestScoped
public class B {
    @ManagedProperty(value = "#{a}")
    private A a;
}

【讨论】:

  • 好的,我会尝试回复。
【解决方案2】:

我刚刚在p:commandButton中添加了update=":receiptViewForm",也就是对话框的表单id。

<p:commandButton icon="ui-icon-search" title="View"
process="@this" oncomplete="receiptViewWidget.show()"
action="#{receiptRepoMB.forReceiptDialog}"
update=":receiptViewForm">
<f:setPropertyActionListener target="#{receiptRepoMB.receiptDetObj}" 
value="#{rd}" />
</p:commandButton>

而且效果很好。

【讨论】:

    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    相关资源
    最近更新 更多