tags:

views:

645

answers:

2

What is the difference between the value and itemValue attribute of the radiobutton in Jsf?

A: 
VonC
Those images are copyright, and should not be used without permission.
jmanning2k
+2  A: 

The value is meant to send in a SelectItem object and not a String like itemValue is. The itemValue is the items value, which is passed to the server as a request parameter, but the value is a value binding expression that points to a SelectItem instance.

If you look at this JSF:

 <h:selectOneRadio value="">
    <f:selectItem itemValue="TestValue" itemLabel="TestLabel" />
</h:selectOneRadio>

which turns into this HTML:

<table>
    <tr>
    <td>
        <input type="radio" name="j_id_id9" id="j_id_id9:0" value="TestValue" />
        <label for="j_id_id9:0"> TestLabel</label>
    </td>
    </tr>
</table>

So value = valueBinding pointing to a SelectItem in your managed bean, and itemValue = the value that is being submitted. If you decided to add a value="#{TestBean.mySelectItem}" it would not change the outputted HTML in any way, but the JSF implementation would know that the getter property for the mySelectItem field should be used on this.

Edit: To clarify the answer a bit more. The value property of the SelectItem binds the SelectItem to an SelectItem field in the managed bean via getter and setter properties. If you set the value like this:

 <h:selectOneRadio value="">
    <f:selectItem itemValue="TestValue" itemLabel="TestLabel" value="#{TestBean.mySelect}"/>
</h:selectOneRadio>

it will invoke the getMySelectItem() method in the TestBean. As you can see this has nothing to do with the itemValue as the itemValue is resposible of setting the value of what goes in the request when the user submits the form. The itemValue will then be stored in the h:selectOneRadio's value which hopefully you have bound up to a String field like this:

<h:selectOneRadio value="#{TestBean.selectedRadioValue}">
<f:selectItem itemValue="1" itemLabel="1. radio one" />
<f:selectItem itemValue="2" itemLabel="2. radio two" />
</h:selectOneRadio>

Now if the user checks the radio which to him looks like: "1. radio one" the value "1" will be stored in the TestBean's variable called selectedRadioValue

ChrisAD
I think you've got a better (original) answer here, but I've read that first sentence 4 times, and still have trouble understanding it. Clarify?
jmanning2k
I agree with Jon (on the "better" answer part). +1
VonC
Ive added more info now =) Hopefully this will clarify things
ChrisAD