2

When using plain HTML radio buttons bound to a bean using the jsf: attributes added in JSF 2.2, I run into an issue where the generated names of the radio inputs don't match:

<label>
     <input type="radio" value="foo" name="a-radio" jsf:id="fooOption" jsf:value="#{fooBean.value} />
</label>
<label>
     <input type="radio" value="bar" name="a-radio" jsf:id="barOption" jsf:value="#{fooBean.value} />
</label>

However, when the page is rendered, the name attributes of the inputs become "[some:jsf:id]:fooOption" and "[some:jsf:id]:barOption", meaning that checking one doesn't uncheck the other! Is this a bug, or does the jsf: attribute namespace not support radio buttons?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Chris Cooper
  • 157
  • 1
  • 7

2 Answers2

4

Specify the name as passthrough attribute instead. It will override the implicit attribute.

<html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">

<label>
    <input type="radio" value="foo" a:name="a-radio" jsf:id="fooOption" />
</label>
<label>
    <input type="radio" value="bar" a:name="a-radio" jsf:id="barOption" />
</label>

You only need to redeclare it as <f:viewParam name="a-radio" value="#{fooBean.value}">, or to manually grab the submitted value from the request parameter map.

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

You can better use a h:selectOneRadio component which then contains a series of s:selectItems.

<h:selectOneRadio value="#{fooBean.value}">
    <f:selectItem itemValue="foo" itemLabel="foo" />
    <f:selectItem itemValue="bar" itemLabel="bar" />
</h:selectOneRadio>

For a more complete sample see http://www.mkyong.com/jsf2/jsf-2-radio-buttons-example/

Eelke
  • 20,897
  • 4
  • 50
  • 76
  • 3
    OP want to customize the default radio button rendering which isn't possible with `` (it produces a HTML `` by default)
    – BalusC Aug 10 '15 at 10:01