I have ap: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,页面会显示实际情况。