0

I have a selectOneRadio that updates a selectOneList, using ajax:

<h:outputLabel for="categ" value="Category" />
<h:selectOneRadio id="categ" value="#{cdManager.store.cd.categ}" required="true">
    <f:selectItems value="#{cdManager.store.cd.categs}"/>                    
    <f:ajax execute="categ" render="subcategs" listener="#{cdManager.categChanged}" />
</h:selectOneRadio>                                

<h:outputLabel for="subcategs" value="Sub-categ"/>
<h:selectOneListbox id="subcategs" value="#{cdManager.store.cd.subcateg}"     required="true">
    <f:selectItems value="#{cdManager.subcategsArray}"/>                    
</h:selectOneListbox>

When the page is presented, my session bean creates a new (empty) CD object. When the user fills all info and submits, the (now filled) CD object is saved, that works fine.

The problem is that the 'categ' property is being set when the user changes the radio (withou submit) and this is bad because the user can click the 'back browser' and when they come back, the radio will be selected and the other fields will empty, very bad behaviour!

How can i solve this and still use ajax?

Here is the listener code:

public void categChanged()
{        
    //store.cd.categ is alredy set ? WHYYY ?
    String categ = store.getCd().getCateg();    
    this.subcategsArray = store.getCd().getSubcateg(categ);                                                 
}

I'm using JSF 2.0, NetBeans IDE 7.0.1 with Glassfish 3.1, any help would be great, thanks!

Fernando
  • 7,785
  • 6
  • 49
  • 81

1 Answers1

1

The problem is that the 'categ' property is being set when the user changes the radio (without submit)

That is the point of ajax.

If you use the browser's back button you will see a page from the browser cache. You could disable caching of your page. Then you would get a new page from the server with a new backing bean (if it is request or view scoped) or check for the presence of other fields in your categChanged() method.

Community
  • 1
  • 1
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • I already tried to disable caching, but it's a Session bean. I also need ajax because the list may grow. All i want is to 'synchronize' the bean with the form. In other words, if the user fills the form and reload the page, a blank form should appear. – Fernando Apr 12 '12 at 06:05
  • Is it really necessary to have the bean in session scope? Your requirements rather suggest one of the narrower scopes. – Matt Handy Apr 12 '12 at 06:15
  • Ok. Then why not "preparing" the bean for the new entries during submit action (the same as if you prepare it the first time)? – Matt Handy Apr 12 '12 at 06:20
  • No, if you do a submit you call an action method. From this action method you could "re-prepare" your bean fields. – Matt Handy Apr 12 '12 at 06:36
  • No binding. As you write in your question _"session bean creates a new (empty) CD object"_. You should create a new empty object from your submit action method. – Matt Handy Apr 12 '12 at 06:41
  • Yes i already do this, each submit a new CD is put to edit. The problem is the 'browser back button' that causes the radio button to be selected if the user hit 'back' then 'forward'. This happens because the ajax event sets the bean 'category', that's the cause of the problem. – Fernando Apr 12 '12 at 06:50
  • Well i give up trying to solve this back button issue, but thanks anyway. – Fernando Apr 12 '12 at 07:18