gpt4 book ai didi

jsf - renders table element, 如何避免这种情况?

转载 作者:行者123 更新时间:2023-12-02 02:53:16 31 4
gpt4 key购买 nike

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

感谢任何帮助!

最佳答案

JSF 2.3 与 group属性

根据 JSF spec issue 329我添加了一个新的 group属性为<h:selectOneRadio>这应该会让这一切不再那么乏味。所有单选按钮组件都具有相同的 group父级内的值 UIForm将被彼此分组。此外,如果选择项具有非空 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 child 不存在,那么它将引用组中第一个组件的子组件。

<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 的功能属性作为直通属性。为了在模型中设置提交的值,只需要额外添加<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 。这只是一点点工作。从下面显示的第一个“另请参阅”链接开始:

另请参阅:

关于jsf - <h :selectOneRadio> renders table element, 如何避免这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7435039/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com