1

I am trying to create a simple dropdown box using Ajax and JSF 2.0/primeface. Based on First Dropdown selection second dropdown box is populated using AJAX call.

When I select first drop down it correctly populated the second dropdown box based on the Ajax call. But When I make selection in the second dropdown and click the button {which basically submit the form for some action}, It give error message "formViewBusinessCode:selectedBusinessCode: Validation Error: Value is not valid"

When I check in console that is says the value for "selectedBusinessCode"{Id of second dropdown} is null. I am puzzled becuase it populates correctly but only after selection it gives error that value is not valid (basically null), Why the selected value is not reaching to the bean? Can someone please point what I am missing here, TIA

xhtml code is as below

<h:outputText styleClass="outputText" value="#{constant.businessCodeGroup}"></h:outputText>
<h:selectOneMenu id="selectedBusinessCodeGroup" value="#{viewBusinessCodeBean.selectedBusinessCodeGroup}" >
    <f:selectItem itemValue="SELCT" itemLabel="Select Business Code Group" />
    <f:selectItems value="#{viewBusinessCodeBean.businessCodeGroupList}" />
    <p:ajax listener="#{viewBusinessCodeBean.getOnlyBusinessCodeListByAjaxCall}" event="change" update="selectedBusinessCode" process="@this"/>
</h:selectOneMenu> 

<h:outputText styleClass="outputText" value="#{constant.businessCode}"></h:outputText>
    <h:selectOneMenu id="selectedBusinessCode" value="#{viewBusinessCodeBean.selectedBusinessCode}">
    <f:selectItem itemValue="SELCT" itemLabel="Select Business Code" />
    <f:selectItems value="#{viewBusinessCodeBean.businessCodeList}" itemLable="#{viewBusinessCodeBean.businessCodeList.getlable}"
    itemValue="#{viewBusinessCodeBean.businessCodeList.getValue}" />
</h:selectOneMenu>

<h:commandButton value="View" action="#{viewBusinessCodeBean.getOnlyBusinessCodeDescription}"></h:commandButton>

The bean coding is as below. it is a @ManagedBean

To Populate First Dropdown box

public ViewBusinessCodeBean() {
    logger.entering(CLASS_NAME);
    this.businessCodeGroupList = new ArrayList<SelectItem>();
    List<String>tempBusinessCodeList = new BusinessCodeTableServices().getBusinessCodeGroupList();
    Iterator<String>iterator = tempBusinessCodeList.iterator();
    while(iterator.hasNext()){
        String businessCodeGroup = iterator.next();
        logger.debug(businessCodeGroup);
        SelectItem item = new SelectItem(businessCodeGroup);
        businessCodeGroupList.add(item);
    }
    logger.exiting(CLASS_NAME);
}

Ajax Call Method which populated second dropdown

public void getOnlyBusinessCodeListByAjaxCall() {
    this.businessCodeList = new ArrayList<SelectItem>();
    List<String>tempBusinessCodeList = new BusinessCodeTableServices().getOnlyBusinessCodeList(getSelectedBusinessCodeGroup());
    Iterator<String>iterator = tempBusinessCodeList.iterator();
    while(iterator.hasNext()){
        String businessCode = iterator.next();
        SelectItem item = new SelectItem(businessCode,businessCode,businessCode);
        businessCodeList.add(item);
    }   
}
SXV
  • 277
  • 5
  • 16

1 Answers1

0

Your bean is apparently in the request scope. A request scoped bean is reconstructed on every request with all properties set to default. Your validation error is caused because businessCodeList property has become null/empty during the request of processing the form submit.

Putting the bean in the view scope should fix this problem.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks. It fixed the problem but it was giving me another problem for java.io.NotSerializableException: which was solved by keeping the context in web.xml org.apache.myfaces.SERIALIZE_STATE_IN_SESSION false – SXV Feb 22 '13 at 13:47