遍历集合时将动态ID分配给隐藏字段 [英] Assign dynamic ids to hidden fields when iterating over a collection
问题描述
是否可以将动态ID分配给h:inputHidden
组件?
Is there a way to assign dynamic ids to h:inputHidden
components?
EDIT1
在遍历元素集合时,我试图在ui:repeat
标记内分配ID.
I am trying to assign the ids inside a ui:repeat
tag when iterating over a collection of elements.
推荐答案
无法基于<ui:repeat>
的迭代值设置ID.但是您仍然不需要它.默认情况下,他们将已基于迭代索引获得动态且唯一的ID.
It is not possible to set the ID based on the iteration value of an <ui:repeat>
. But you don't need it anyway. They will by default already get dynamic and unique IDs based on the iteration index.
例如
<h:form id="form">
<ui:repeat value="#{bean.list}" var="item">
<h:inputHidden id="hidden" value="#{item.value}" />
</ui:repeat>
</h:form>
将在视图渲染期间生成此HTML
will generate this HTML during view render time
<form id="form" name="form">
<input type="hidden" id="form:0:hidden" name="form:0:hidden" value="item1value" />
<input type="hidden" id="form:1:hidden" name="form:1:hidden" value="item2value" />
<input type="hidden" id="form:2:hidden" name="form:2:hidden" value="item3value" />
</form>
如果要手动控制ID,则需要使用<c:forEach>
,因为<ui:repeat>
不会生成多个JSF组件,而是允许其子级(在上面是单个<h:inputHidden>
例如)多次生成HTML. <c:forEach>
将生成多个JSF组件,然后每个组件仅生成一次HTML(因此,您实际上在JSF组件树中最终拥有多个<h:inputHidden>
组件).
If you want to manually control the ID, you'd need to use <c:forEach>
instead, because <ui:repeat>
doesn't generate multiple JSF components, but lets its children (which is a single <h:inputHidden>
in the above example) generate HTML multiple times. The <c:forEach>
will generate multiple JSF components which then each generate HTML only once (so you effectively end up with multiple <h:inputHidden>
components in JSF component tree).
例如
<h:form id="form">
<c:forEach items="#{bean.list}" var="item">
<h:inputHidden id="#{item.id}" value="#{item.value}" />
</c:forEach>
</h:form>
基本上将在视图构建期间生成此JSF组件树
which will basically generate this JSF component tree during view build time
<h:form id="form">
<h:inputHidden id="item1id" value="#{bean.list[0].value}" />
<h:inputHidden id="item2id" value="#{bean.list[1].value}" />
<h:inputHidden id="item3id" value="#{bean.list[2].value}" />
</h:form>
这将在视图渲染期间生成此HTML
which in turn will generate this HTML during view render time
<form id="form" name="form">
<input type="hidden" id="form:item1id" name="form:item1id" value="item1value" />
<input type="hidden" id="form:item2id" name="form:item2id" value="item2value" />
<input type="hidden" id="form:item3id" name="form:item3id" value="item3value" />
</form>
另请参见:
- JSF2 Facelets中的JSTL ...有意义吗?
- JSTL in JSF2 Facelets... makes sense?
See also:
这篇关于遍历集合时将动态ID分配给隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!