0

I have a simple form.

<h:form>
   <h:selectOneMenu value="#{user.siteID}" >
       <f:selectItems id="vals" value="#{user.basinSiteIDs}" /> 
       <f:ajax event="valueChange" listener="#{user.updateWithAjax(e)}"
                render="all" />
   </h:selectOneMenu>
   <h:selectManyCheckbox id="all" value="#{user.siteIDs}" layout="pageDirection">
       <f:selectItems id="sites" value="#{user.csrpSites}" />
   </h:selectManyCheckbox>
   <h:commandButton value="submit" action="result"/>
</h:form>

The page initially loads with a drop down and check boxes with associated values. When I make a selection from the drop down, the check box values are changed dynamically with ajax. I need to click submit button and display the user selected values in result page.

Here is the problem: If I use @RequestScoped, clicking the submit button gives j_idt7:all: Validation Error: Value is not valid.

@ViewScoped, takes to result page but with empty/null values.

@SessionScoped, shows result page with correct values but they are gone when I click browser's back button and land in the index page. This happens only under IE and Chrome but not in Firefox.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
suj smith
  • 13
  • 3
  • If you want to have `user` available on the next page `@ViewScoped` is too narrow (it's only keeping state for your index page). I don't know how JSF handles back-buttons off the top of my head. So better wait a bit for somebody to shed light on this. Also, here's another [useful answer](http://stackoverflow.com/a/7031941/785663) – mabi Aug 06 '13 at 17:12
  • Please post you bean code! – Kishor Prakash Aug 07 '13 at 04:24

1 Answers1

0

The @ViewScoped is the right scope for the purpose of having a dependent dropdownlist which is populated by ajax. Your concrete problem is caused by binding one same view scoped bean to 2 physically different views for some reason. A view scoped bean lives as long as the view itself. If you change the view, then you'll get a new view scoped bean. If you had shown the results in the same view, then it would have worked just fine.

If you really need to keep this odd approach of 2 physically different views, then your best bet is to split the bean in two:

<h:selectOneMenu id="basin" value="#{user.basinSiteID}" >
    <f:selectItems value="#{data.basinSiteIDs}" /> 
    <f:ajax listener="#{data.loadCsrpSiteIDs}" render="csrp" />
</h:selectOneMenu>
<h:selectManyCheckbox id="csrp" value="#{user.csrpSiteID}" layout="pageDirection">
    <f:selectItems value="#{data.csrpSiteIDs}" />
</h:selectManyCheckbox>
<h:commandButton value="submit" action="result"/>

(note that I did some improvements here, your initial code was somewhat dirty and particularly the attempt to pass ajax behavior event is completely wrong, it would arrive as null)

The #{user} is here request scoped and #{data} is view scoped.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555