Jsf 在Setter之前调用的事件函数
我有下面的下拉列表,其中列出了几辆车,这样它就可以将所选项目的值存储在backbean变量中,并触发一个事件,以便根据此下拉控件的所选值填充其他下拉列表,如下所示:Jsf 在Setter之前调用的事件函数,jsf,valuechangelistener,Jsf,Valuechangelistener,我有下面的下拉列表,其中列出了几辆车,这样它就可以将所选项目的值存储在backbean变量中,并触发一个事件,以便根据此下拉控件的所选值填充其他下拉列表,如下所示: <Td> <h:selectOneMenu id="combocarList" value="#{customerBean.selectedcar}" styleClass="comboStyle" valueChangeListener="#{customerBean.loadoth
<Td>
<h:selectOneMenu id="combocarList"
value="#{customerBean.selectedcar}"
styleClass="comboStyle"
valueChangeListener="#{customerBean.loadothercombos}"
onchange="document.forms[0].submit()"
>
<f:selectItem
itemLabel="-----------Select--------------"
itemValue="None" />
<f:selectItems value="#{customerBean.carsList}" />
</h:selectOneMenu>
</Td>
问题是,当从上述下拉列表中选择一个项时,会在setter之前调用事件loadothercombos,从而导致问题
请注意,backbean客户的定义如下:
<managed-bean-name>customerBean</managed-bean-name>
<managed-bean-class>com.theway.customer</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
我在调试中看到的行为是,当我从下拉列表中选择一个项目时:
1) Getter is called for selectedcar
2) Loadothercombos is called <------- This is called by the event
3) Setter is called for selectedcar
我无法让它在调用loadothercombos之前调用setter。如有任何见解,将不胜感激。谢谢
问题是当从上面的下拉列表中选择一个项目时,事件
在setter之前调用loadothercombos
这就是预期的JSF生命周期行为。valueChangeListener={customerBean.loadothercombos}在提交值成功转换/验证后的验证阶段调用,并且仅当提交值与初始值不同时调用。调用valueChangeListener后,JSF将继续转换/验证下一个UIInput,当JSF实现确定数据有效时,然后继续下一个更新模型值阶段,调用setter方法值={customerBean.selectedcar}使用valueChangeListener实现这一目的一直是一种很不方便的方法。基本上,有两种方法可以解决这个问题:
调用,以便JSF立即移动到渲染响应阶段,从而跳过更新模型值和调用操作阶段:
public void loadothercombos(ValueChangeEvent event) {
selectedcar = (String) event.getNewValue();
loadOtherCombosBasedOn(selectedcar);
// ...
FacesContext.getCurrentInstance().renderResponse();
}
将事件排队以调用操作阶段,以便在调用setter后执行其任务:
public void loadothercombos(ValueChangeEvent event) {
if (event.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
loadOtherCombosBasedOn(selectedcar);
} else {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
event.queue();
}
}
如果您使用的是JSF 2.0,那么在以下帮助下,有一种更容易处理的方法:
与具体问题无关:combobox是此下拉元素的不正确术语。组合框是一个可编辑的下拉列表,它基本上是和的组合。你在那里看到的只是单独渲染,这些只是下拉列表,所以也可以这样称呼它们
<h:selectOneMenu id="combocarList"
value="#{customerBean.selectedcar}"
styleClass="comboStyle">
<f:selectItem
itemLabel="-----------Select--------------"
itemValue="None" />
<f:selectItems value="#{customerBean.carsList}" />
<f:ajax listener="#{customerBean.loadOtherCombos}" render="otherComboIds" />
</h:selectOneMenu>
public void loadothercombos() {
loadOtherCombosBasedOn(selectedcar);
}