1

I use the <h:selectOneRadio> component in JSF2 to create radiobuttons. One is "Yes" and another one "No". Note that I cannot use a checkbox here.

In my backing bean I have a boolean property. When "yes" is selected, I want that boolean to be true and when "no" is selected I want it to be false. How can I achieve this?

This doesn't do it:

<h:selectOneRadio value="#{bean.bool}">
    <f:selectItem itemValue="false" itemLabel="Yes" />
    <f:selectItem itemValue="true" itemLabel="No" />
    <f:ajax event="change" render="theDiv" />
</h:selectOneRadio>
<h:panelGroup layout="block" id="theDiv" rendered="#{bean.bool}">
....
</panelGroup>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Works fine for me. What exactly is your concrete problem? You didn't describe the concrete problem at all when using this code snippet. – BalusC Sep 25 '12 at 14:46
  • Maybe you want to use `itemValue="#{false}"`? – Elias Dorneles Sep 25 '12 at 14:47
  • @eljunior: EL will automatically coerce it. – BalusC Sep 25 '12 at 14:52
  • I want the bean.bool property to switch between true and false depending on what radiobutton is selected... It's clearly stated in my question! – Ralf the la Vega Sep 25 '12 at 15:55
  • @RalfthelaVega Well, describe the exact symptom of the problem, eg., how you are actually testing the value of the bool property. – Elias Dorneles Sep 25 '12 at 16:17
  • @eljunior I think the problem is that i dont understand how everything is connected. I want to show/hide a div in a depending on the selection of the radiobuttons. I do this with the "rendered"attribute of a panelGroup. Everytime you change the selection of the radiobuttons the panelGroup(div) gets rendered depending on the bean.bool... I hope this makes it clearer – Ralf the la Vega Sep 25 '12 at 16:21
  • @eljunior I have edited the question some more – Ralf the la Vega Sep 25 '12 at 16:26
  • 1
    If you have run the debugger or at least added poor man's `System.out.println()` lines, then you should have noticed that the setter method is properly invoked, hereby making your initial doubt completely invalid. The cause of your current problem is now after the code update however crystal clear. I'll post an answer. In the future, [try posting a real SSCCE](http://stackoverflow.com/tags/jsf/info) instead of making unclear assumptions as in "it doesn't work". Try to investigate and elaborate the problem in developer's perspective instead of in enduser's perspective. – BalusC Sep 25 '12 at 16:39

1 Answers1

0

JSF runs in the webserver and generates HTML. Ajax/JavaScript runs in the webbrowser and works on the JSF-generated HTML output. You can't ajax-update the HTML representation of a JSF component which is by itself never rendered to HTML. You need to ajax-update the HTML representation of a JSF component which is always rendered:

<h:selectOneRadio value="#{bean.bool}">
    <f:selectItem itemValue="false" itemLabel="Yes" />
    <f:selectItem itemValue="true" itemLabel="No" />
    <f:ajax render="foo" />
</h:selectOneRadio>
<h:panelGroup id="foo">
    <h:panelGroup layout="block" id="theDiv" rendered="#{bean.bool}">
    ...
    </panelGroup>
</h:panelGroup>

(note that I removed event="change" as this is wrong and the default one is already right)

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I understand the problem, pretty cool actually. But still, when i run my debugger the setter of the bool-property is never called. Everytime I change selection the getter is called but not the setter. Further suggestions? – Ralf the la Vega Sep 25 '12 at 17:31
  • This is not answerable based on the information provided so far. – BalusC Sep 25 '12 at 17:45
  • @RalfthelaVega If the setter is not called, the only thing I can think of is that you may be lacking an `h:form` wrapping everything. Maybe you could post the full code used for your page. – Elias Dorneles Sep 25 '12 at 20:43