<h:selectOneMenu> ValueChangeListener Problem
- by Ehsun
I have two <h:selectOneMenu> tags on my form like this:
<h:outputText value="#{lbls.incomeType}:"/>
<h:selectOneMenu id="incomeType" label="#{lbls.incomeType}" onchange="submit();" valueChangeListener="#{price.earnTypeValueChanged}" immediate="true">
<f:selectItems value="#{price.earnTypes}"/>
<f:converter converterId="ir.khorasancustoms.EarnTypeConverter"/>
</h:selectOneMenu>
<h:message for="incomeType" infoClass="info" errorClass="error" warnClass="warning" fatalClass="fatal"/>
<h:outputText value="#{lbls.earnAttribute}:"/>
<h:selectOneMenu id="earnAttribute" label="#{lbls.earnAttribute}" value="#{price.price.earnAttribute}">
<f:selectItems value="#{price.earnAttributes}"/>
<f:converter converterId="ir.khorasancustoms.EarnAttributeConverter"/>
</h:selectOneMenu>
<h:message for="incomeType" infoClass="info" errorClass="error" warnClass="warning" fatalClass="fatal"/>
<h:messages globalOnly="true" layout="table" infoClass="info" warnClass="warning" errorClass="error" fatalClass="fatal"/>
<h:button value="#{lbls.cancel}" outcome="index"/>
<h:commandButton value="#{lbls.ok}" action="#{price.save}"/>
the handler in the bean looks like this:
public void earnTypeValueChanged(ValueChangeEvent event) {
EarnType newEarnType = (EarnType) event.getNewValue();
earnAttributes = newEarnType.getAttributes();
FacesContext.getCurrentInstance().renderResponse();
}
Everything works fine, except the user needs to press the OK button twice because the first one fires the earnTypeValueChanged. How is that caused and how can I fix it?
Update 1:
public void earnTypeValueChanged(ValueChangeEvent event) {
EarnType oldEarnType = (EarnType) event.getOldValue();
EarnType newEarnType = (EarnType) event.getNewValue();
earnAttributes = newEarnType.getAttributes();
if (!newEarnType.equals(oldEarnType)) {
FacesContext.getCurrentInstance().renderResponse();
}
}