<h:selectOneMenu> ValueChangeListener Problem

Posted by Ehsun on Stack Overflow See other posts from Stack Overflow or by Ehsun
Published on 2011-01-10T18:56:05Z Indexed on 2011/01/11 5:53 UTC
Read the original article Hit count: 169

Filed under:
|

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();
    }
  }

© Stack Overflow or respective owner

Related posts about jsf

Related posts about jsf-2.0