将输入类型="radio" 指定为 JSF 直通元素时覆盖名称属性

name attribute overriden when specifying input type="radio" as JSF passthrough element

在使用 JSF 2.2 中添加的 jsf: 属性将纯 HTML 单选按钮绑定到 bean 时,我 运行 遇到了单选输入的生成名称不匹配的问题:

<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>

但是,当页面呈现时,输入的名称属性变为“[some:jsf:id]:fooOption”和“[some:jsf:id]:barOption”,这意味着检查一个不会不要取消选中另一个!这是一个错误,还是 jsf: 属性命名空间不支持单选按钮?

您最好使用 h:selectOneRadio 组件,它包含一系列 s:selectItem

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

有关更完整的示例,请参阅 http://www.mkyong.com/jsf2/jsf-2-radio-buttons-example/

name 指定为 passthrough 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>

只需要重新声明为<f:viewParam name="a-radio" value="#{fooBean.value}">,或者手动从请求参数映射中抓取提交的值即可