有没有一种方法可以告诉JSF在使用<table>时不应渲染<h:selectOneRadio>元素?
我不使用表格,在这种情况下绝对没有意义。

任何帮助表示赞赏!

最佳答案

具有group属性的JSF 2.3

根据JSF spec issue 329,我在group中添加了一个新的<h:selectOneRadio>属性,这将使这一切变得不那么繁琐。父group中具有相同UIForm值的所有单选按钮组件将彼此分组。此外,如果选择项具有非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>

以下方案也是可能的。如果存在多个具有相同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" />
<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" />
<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>
<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>

根据2.3.0-m07,它将在Mojarra中可用。

带有直通元素的JSF 2.2

如果您已经在使用JSF 2.2,请利用其新的passthrough elements/attribtues功能,通过该功能将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}" />

可以在以下博客中找到深入的技术说明:Custom layout with h:selectOneRadio in 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>

您还可以循环可用的项目,只需要在view build time期间进行即可:
<ul>
    <c:forEach items="#{bean.availableFoos}" varStatus="loop">
        <li><p:radioButton for="foo" itemIndex="#{loop.index}" /></li>
    </c:forEach>
</ul>

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

如果您尚未使用JSF 2.2,或者您不喜欢PrimeFaces UI,请捕获Tomahawk's <t:selectOneRadio> ,它与<h:selectOneRadio>呈现相同的纯HTML输出,但支持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>

自定义渲染器

提供自定义 Renderer 。这只是一点点的工作。从下面显示的第一个“另请参阅”链接开始:

也可以看看:
  • How to override h:selectOneRadio renderer? Where is the renderer class in jsf-impl?
  • Is it possible to use JSF to build clean CSS layouts without using tables?
  • Render selectManyCheckbox without HTML table
  • 10-07 13:07
    查看更多