GeeCON Prague 2014*
The moose likes JSF and the fly likes dynamic selectOneRadio Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "dynamic selectOneRadio" Watch "dynamic selectOneRadio" New topic
Author

dynamic selectOneRadio

Fernando Guerrero
Greenhorn

Joined: Apr 26, 2011
Posts: 14

Hello

I have an arrayList n size, it contains users names, I want to display them and be able to choose one user. To display this users into radio buttons I am using a dataTable to iterate through the arrayList. Then when the user clicks on one radio button the form is submitted, and a method that detects a change in the value is called and the value is saved. The problem that I have is that if I have n elements in my array, and those n elements are displayed , when I click on one radio button, the form is submitted only one time ( so far so good) but the changedValue method is called n times!!!
This is my xhtml code :



This is my java bean code:




Suppose that I am displaying the next radio buttons in this order:

User1
User2
User3
User4


When the page is displayed the first time, any user is selected , then I choose User1, the new value is saved and the page is rendered showing User1 selected.
This would be the log messages that I get:

NEW USER SELECTED
NEW USER VALUE : User1
NEW USER SELECTED
NEW USER SELECTED
NEW USER SELECTED

As you can see the method repeatValueChanged(ValueChangeEvent event) was called n times.
Then If I choose User3, I get this:
NEW USER SELECTED
NEW USER VALUE : User1
NEW USER SELECTED
NEW USER SELECTED
NEW USER VALUE : User3
NEW USER SELECTED

The User3 value is saved and when the pages is rendered the User3 is selected,
Then if I choose User 2, I would get this:

NEW USER SELECTED
NEW USER SELECTED
NEW USER VALUE : User2
NEW USER SELECTED
NEW USER VALUE : User3
NEW USER SELECTED

Even the User2 is the new value, the beans saves the User3 value, and when the page is rendered again, User3 is selected.


What I am doing wrong???

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16092
    
  21

I'm not sure that it's safe to use the JavaScript submit() function for this. What you really need is AJAX.

Listeners and property setters can, and are, invoked multiple times in the JSF lifecycle. You should never do heavy-duty work in listeners. Do the heavy work in the action method. At that point, the radio value will have been automatically updated to the new value.

You don't even need a listener unless you want specifically to know in the action method that the radio selection changed and possibly what it changed from. If that's the case, it's sufficient for the listener to set an internal property with the "changed" boolean indicator and/or the previous value. That's a lightweight function with no side-effects, so it's safe for listeners to do.

Listeners and bindings tend to be overused in JSF. I think it's because there weren't better ways originally and the outdated docs are still floating around.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic selectOneRadio