<h:selectOneRadio>渲染表格元素,如何避免这种情况?

m4r*_*ri0 18 jsf html-table selectoneradio

有没有办法告诉JSF它<table>在使用时不应该呈现元素<h:selectOneRadio>?我不使用表格,在这种情况下它完全没有意义.

任何帮助表示赞赏!

Bal*_*usC 23

带有group属性的JSF 2.3

根据JSF规范问题329,我添加了一个新group属性,<h:selectOneRadio>这应该使这一切都不那么繁琐.group在父母中具有相同值的所有单选按钮组件UIForm将彼此分组.除了单选按钮本身和可选标签之外,如果select项具有非null,它们也不会呈现任何标记label.如果有,标签会直接出现在单选按钮之后.

<!-- Just markup them the way you want! -->
<ul>
    <ui:repeat id="items" value="#{bean.items}" var="item">
        <li>
            <h:selectOneRadio group="foo" value="#{bean.selectedItem}">
                <f:selectItem itemValue="#{item}" />
            </h:selectOneRadio>
        </li>
    </ui:repeat>
</ul>
Run Code Online (Sandbox Code Playgroud)

以下场景也是可能的.当存在具有相同的多个部件group,并且所述value属性和/或UISelectItem子不存在,那么它会引用那些的基团的第一组分.

<h:selectOneRadio group="foo" value="#{bean.selectedItem}">
    <f:selectItems value="#{bean.availableItems}" />
</h:selectOneRadio>
<h:selectOneRadio group="foo" />
<h:selectOneRadio group="foo" />
Run Code Online (Sandbox Code Playgroud)

<h:selectOneRadio group="foo" value="#{bean.selectedItem}">
    <f:selectItem itemValue="one" />
    <f:selectItem itemValue="two" />
    <f:selectItem itemValue="three" />
</h:selectOneRadio>
<h:selectOneRadio group="foo" />
<h:selectOneRadio group="foo" />
Run Code Online (Sandbox Code Playgroud)

<h:selectOneRadio group="foo" value="#{bean.selectedItem}">
    <f:selectItem itemValue="one" />
</h:selectOneRadio>
<h:selectOneRadio group="foo">
    <f:selectItem itemValue="two" />
</h:selectOneRadio>
<h:selectOneRadio group="foo">
    <f:selectItem itemValue="three" />
</h:selectOneRadio>
Run Code Online (Sandbox Code Playgroud)

<h:selectOneRadio group="foo" value="#{bean.selectedItem}">
    <f:selectItem itemValue="one" />
</h:selectOneRadio>
<h:selectOneRadio group="foo" value="#{otherBean.selectedItem}">
    <f:selectItem itemValue="two" />
</h:selectOneRadio>
<h:selectOneRadio group="foo" value="#{lastBean.selectedItem}">
    <f:selectItem itemValue="three" />
</h:selectOneRadio>
Run Code Online (Sandbox Code Playgroud)

它将按照2.3.0-m07在Mojarra中提供.

JSF 2.2带有passthrough元素

如果您已经使用JSF 2.2,请使用其新的passthrough元素/属性功能,您可以将该name属性显式设置为passthrough属性.要在模型中设置提交的值,您只需要一个额外的值<h:inputHidden>.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:jsf="http://xmlns.jcp.org/jsf"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:a="http://xmlns.jcp.org/jsf/passthrough">

...

<!-- Just markup them the way you want! -->
<ul>
    <ui:repeat id="items" value="#{bean.items}" var="item">
        <li>
            <input type="radio" jsf:id="item" a:name="#{hiddenItem.clientId}"
                value="#{item}" a:checked="#{item eq bean.selectedItem ? 'checked' : null}" />
            <h:outputLabel for="item" value="#{item}" />
        </li>
    </ui:repeat>
</ul>

<!-- This one won't display anything. -->
<h:inputHidden id="selectedItem" binding="#{hiddenItem}" value="#{bean.selectedItem}"
    rendered="#{facesContext.currentPhaseId.ordinal ne 6}" />
Run Code Online (Sandbox Code Playgroud)

深入的技术解释可以在这篇博客中找到:自定义布局与h:selectOneRadio在JSF 2.2中.

PrimeFaces(JSF 2.x)

如果你碰巧使用PrimeFaces,那么你也可以使用<p:selectOneRadio layout="custom"><p:radioButton>.

<html ... xmlns:p="http://primefaces.org/ui">

<!-- This one won't display anything. -->
<p:selectOneRadio id="foo" value="#{bean.selectedFoo}" layout="custom">
    <f:selectItems value="#{bean.availableFoos}" />
</p:selectOneRadio>

<!-- Just markup them the way you want! -->
<ul>
    <li><p:radioButton for="foo" itemIndex="0" /></li>
    <li><p:radioButton for="foo" itemIndex="1" /></li>
    <li><p:radioButton for="foo" itemIndex="2" /></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

您还可以遍历可用项目,只需在视图构建期间执行此操作:

<ul>
    <c:forEach items="#{bean.availableFoos}" varStatus="loop">
        <li><p:radioButton for="foo" itemIndex="#{loop.index}" /></li>
    </c:forEach>
</ul>
Run Code Online (Sandbox Code Playgroud)

战斧(JSF 1.x或2.x)

如果您尚未使用JSF 2.2,或者您不喜欢PrimeFaces UI,请抓住Tomahawk <t:selectOneRadio>,它会呈现相同的纯HTML输出<h:selectOneRadio>,但支持layout="spread"属性,以便您可以<t:radio>按照自己的方式定位项目.

例如

<html ... xmlns:t="http://myfaces.apache.org/tomahawk">

<!-- This one won't display anything. -->
<t:selectOneRadio id="foo" value="#{bean.selectedFoo}" layout="spread">
    <f:selectItems value="#{bean.availableFoos}" />
</t:selectOneRadio>

<!-- Just markup them the way you want! -->
<ul>
    <li><t:radio for="foo" index="0" /></li>
    <li><t:radio for="foo" index="1" /></li>
    <li><t:radio for="foo" index="2" /></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

自定义渲染器

供应定制Renderer.这只是一点工作.从第一个"另请参见"链接开始,如下所示:

也可以看看: