1

I am trying to build an interface with three different select in way that the first select will render the second, this second select will load data and after the user selects an option from the second select, this will render the third that will load its data waiting for the user to select the final option.

After debugging and trying to solve this problem I see the last method is being called using an empty value, I guess it's because the bean is request scoped and after the AJAX request is destroyed. How could I work around this problem without making several calls to the DB and using a non-session-scoped-bean?

<h:selectOneMenu value="#{RequestBean.firstSelectValue}" id="first">
  <f:selectItem id="default" itemLabel="Select one" itemValue="-1" />
  <f:selectItems value="#{RequestBean.firstSelectElements}" 
                 var="var" itemLabel="#{var.label}"
                 itemValue="#{var.value}" />
  <f:ajax event="change" render="second" onevent="selectListener"/>
</h:selectOneMenu>

<h:selectOneMenu value="#{RequestBean.secondSelectValue}" id="second">
  <f:selectItem id="default" itemLabel="Select one" itemValue="-1" />
  <f:selectItems value="#{RequestBean.secondSelectElements}" 
                 var="var" itemLabel="#{var.label}"
                 itemValue="#{var.value}" />
  <f:ajax event="change" render="third" onevent="selectListener/>
</h:selectOneMenu>

<h:selectOneMenu value="#{RequestBean.third SelectValue}" id="third">
  <f:ajax event="change" render="someTextArea" onevent="selectListener" />
  <f:selectItem id="default" itemLabel="Select one" itemValue="-1" />
  <f:selectItems value="#{RequestBean.getThirdSelectElements(RequestBean.secondSelectValue)}" 
                 var="var" itemLabel="#{var.label}"
                 itemValue="#{var.value}" />
</h:selectOneMenu>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Alibrada
  • 35
  • 5

1 Answers1

1

Just put the bean in the view scope. It'll live as long as you're interacting with the same view by ajax.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have already thought about that as well but I am using CDI Beans. Could it be possible to use two different beans? One will hold the data from the selects and the other will make the data requests from the AJAX calls. – Alibrada Jun 24 '13 at 14:02
  • 1
    Use then the CDI alternative to JSF view scope. See also e.g. http://stackoverflow.com/questions/14158885/add-items-to-list-in-request-scoped-bean/14158970#14158970 Point being, your bean is simply in the wrong scope and you simply need to put it in the right scope. It should live exactly as long as you need it to. – BalusC Jun 24 '13 at 14:05
  • That was also another idea I was trying. Thanks a lot! This is a lot of help :) – Alibrada Jun 24 '13 at 14:08