2
votes

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?

2

2 Answers

4
votes

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.

0
votes

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/