【问题标题】:Use single p:ajax to execute multiple actions使用单个 p:ajax 执行多个操作
【发布时间】:2013-10-14 10:14:20
【问题描述】:

I have a p:selectOneMenu in my application, and when the selection takes place I need to call multiple back-end methods from different managed beans in order to perform different actions.

XHTML 代码:

<p:selectOneMenu id="selectMenu" value="#{userBean.selectedSite}"
  converter="siteConverter" style="width:150px">
  <p:ajax event="change" listener="#{bean2.changeSite}"
    render="@form :comp1 :comp2 :comp3 :comp4" />
  <f:selectItems value="#{userBean.sites}" var="site"
    itemValue="#{site}" itemLabel="#{site.description}" />
  <p:ajax event="change" listener="#{bean1.reset}" 
    update="@form :comp1 :comp2 :comp3 :comp4" />
</p:selectOneMenu>

托管 bean 1:

@ManagedBean(name="bean1")
@ViewScoped
public class Bean1 implements Serializable {
  // ...
  public void reset() {
    loadEvents();
    resetEvent();
  }
}

托管 bean 2:

@ManagedBean(name="bean2")
@SessionScoped
public class Bean2 implements Serializable {
  // ...
  public void changeSite() {
    FacesContext context = FacesContext.getCurrentInstance();
    Bean1 bean = (Bean1) context.getApplication().evaluateExpressionGet(context, "#{bean1}", Bean1.class);
    reload();
    bean.loadEvents();
  }
}

如果我不使用两个不同的 p:ajax 组件,而是使用从 Bean1 调用单个方法的单个 p:ajax,则“更新”下列出的页面组件不会正确更新。

XHTML:

<p:ajax event="change" listener="#{bean1.singleMethod}" 
  update="@form :comp1 :comp2 :comp3 :comp4" />

托管 bean 1:

@ManagedBean(name="bean1")
@ViewScoped
public class Bean1 implements Serializable {
  // ...
  public void () singleMethod() {
    FacesContext context = FacesContext.getCurrentInstance();
    Bean2 bean = (Bean2) context.getApplication().evaluateExpressionGet(context, "#{bean2}", Bean2.class);
    bean2.changeSite();
    reset();
  }
}

更改选择的值会更新服务器端对象,但页面没有更新:如果我按F5,页面显示实际情况。

【问题讨论】:

    标签: ajax jsf primefaces


    【解决方案1】:

    将单个全局方法移至会话范围的 bean (Bean2) 即可完成这项工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多