简体   繁体   English

JSF2 EL未显示在组件ID中

[英]JSF2 EL not displayed in components IDs

I have the following piece of code: 我有以下代码:

<ui:repeat var = "ctr" value = "#{bean.counterList}">
    <h:outputLabel for = "message#{ctr}" value = "#{appMessage['No #{ctr} :" />
<h:inputText id="message#{ctr}" value="#{bean.messageList}" />
</ui:repeat>

The counterList is an List<String> . counterListList<String> If the list contains 1, 2, 3 on the view thee should be 3 input fields with the ids : message1, message2, message3 . 如果列表在视图中包含1, 2, 3应该是3个输入字段,其中包含ids: message1, message2, message3

The EL has no effect in the id attribute and all the components receive just message as an ID. EL对id属性没有影响,所有组件只接收消息作为ID。 On the other hand, in the label's value, the EL works great. 另一方面,在标签的价值中,EL效果很好。

I can imagine that this may be the desired behaviour but is there a workaround? 我可以想象这可能是理想的行为但有解决方法吗?

UPDATE: 更新:

I removed the id attribute and the ui:repeat is in charge of naming the id's now. 我删除了id属性, ui:repeat负责现在命名id。 From the source code I can see that the ID's generated are unique but now this warning is thrown: 从源代码我可以看到生成的ID是唯一的,但现在抛出了这个警告:

INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=fm-story:j_idt103:0:j_idt54[severity=(ERROR 2), summary=(Conversion Error setting value '' for 'null Converter'.), detail=(Conversion Error setting value '' for 'null Converter'.)]
sourceId=fm-story:j_idt103:1:j_idt54[severity=(ERROR 2), summary=(Conversion Error setting value '' for 'null Converter'.), detail=(Conversion Error setting value '' for 'null Converter'.)]

I haven't checked it, but the correct behaviour is going to be of the form: 我没有检查它,但正确的行为将是以下形式:

<ui:repeat var = "ctr" value = "#{bean.counterList}">
  <h:outputLabel for="message" value="#{appMessage['No #{ctr} :" />
  <h:inputText id="message" value="#{bean.messageList}" />
</ui:repeat>

The component identifier message is not going to change; 组件标识符 message不会改变; the client identifier (eg fm-story:j_idt103:0:message ) will change on a per-row basis during lifecycle processing. 客户端标识符 (例如, fm-story:j_idt103:0:message )将在生命周期处理期间逐行更改。 The label component's for attribute algorithm will be able to find the input component using "message" as they share a naming container. 标签组件for属性算法将能够找到使用“消息”,因为它们共享一个命名容器输入组件。

its same issue that i had... How can i set id of h:panelGroup inside ui:repeat 我有同样的问题... 如何在ui:repeat中设置h:panelGroup的id

you can't set the ids in the fly with ui:repeat (look at the link above) 你不能用ui重复设置id:重复(看看上面的链接)

You can use the <c:forEach 您可以使用<c:forEach

like this 像这样

<c:forEach var = "ctr" items = "#{bean.counterList}">
    <h:outputLabel for = "message#{ctr}" value = "#{appMessage['No #{ctr} :" />
    <h:inputText id="message#{ctr}" value="#{bean.messageList}" />
</c:forEach>

(BUT you should only be well aware of how JSTL works in Facelets) (但你应该只清楚 JSTL如何在Facelets中工作)

You should not attempt to manually index the IDs of the components inside a fullworthy JSF iterating component like <ui:repeat> , <h:dataTable> , etcetera. 您不应该尝试手动索引完整的JSF迭代组件内的组件的ID,如<ui:repeat><h:dataTable>等。 This will only result in disaster as you experienced yourself. 这只会导致你经历过的灾难。 It will work inside a JSTL <c:forEach> , because it runs as being a view build time tag at the moment the JSF component tree is to be built, not at the moment the HTML output is to be generated. 它将在JSTL <c:forEach>运行,因为它在构建JSF组件树时作为视图构建时标记运行,而不是在生成HTML输出时。 The <c:forEach> would also generate physically as much JSF components as is been iterated. <c:forEach>也会生成与迭代一样多的JSF组件。

Just omit any EL from id attribute. 只需省略id属性中的任何EL。 The JSF component will worry about setting the right client ID automagically. JSF组件将担心自动设置正确的客户端ID。 Further, you can use varStatus attribute of the <ui:repeat> to get the current loop count and index. 此外,您可以使用<ui:repeat> varStatus属性来获取当前循环计数和索引。 This way you don't need 2 lists. 这样您就不需要2个列表。 The count will show the current round. 计数将显示当前轮次。 The index is mandatory in order to get/set the value of a List<String> by index. 索引是必需的,以便通过索引获取/设置List<String>的值。 The <h:inputText value="#{message}" /> namely won't work because the String class doesn't have a setter. <h:inputText value="#{message}" />即不起作用,因为String类没有setter。

<ui:repeat value="#{bean.messageList}" var="message" varStatus="loop">
    <h:outputLabel for="message" value="#{appMessage['No']} #{loop.count}:" />
    <h:inputText id="message" value="#{bean.messageList[loop.index]}" />
</ui:repeat>

(you can if necessary omit var="message" from the above snippet as it isn't used anywhere) (如果需要,可以从上面的代码段中省略var="message" ,因为它在任何地方都没有使用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM