JSF2 EL未显示在组件ID中 [英] JSF2 EL not displayed in components IDs

查看:57
本文介绍了JSF2 EL未显示在组件ID中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<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>

counterListList<String>.如果列表在视图上包含1, 2, 3,则应为3个ID为message1, message2, message3的输入字段.

The counterList is an List<String>. If the list contains 1, 2, 3 on the view thee should be 3 input fields with the ids : message1, message2, message3.

EL在 id 属性中无效,并且所有组件仅接收消息作为ID.另一方面,就标签的价值而言,EL的效果很好.

The EL has no effect in the id attribute and all the components receive just message as an ID. On the other hand, in the label's value, the EL works great.

我可以想象这可能是理想的行为,但是有解决方法吗?

I can imagine that this may be the desired behaviour but is there a workaround?

更新:

我删除了id属性,并且ui:repeat负责现在命名ID.从源代码中,我可以看到生成的ID是唯一的,但现在会抛出此警告:

I removed the id attribute and the ui:repeat is in charge of naming the id's now. From the source code I can see that the ID's generated are unique but now this warning is thrown:

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'.)]

推荐答案

您不应尝试手动索引完全有价值的JSF迭代组件(例如<ui:repeat><h:dataTable>等)中的组件ID.这只会导致灾难,因为您经历了自己.它将在JSTL <c:forEach>内部工作,因为它在构建JSF组件树时(而不是在生成HTML输出时)作为视图构建时标签运行. <c:forEach>还会在物理上生成与迭代一样多的JSF组件.

You should not attempt to manually index the IDs of the components inside a fullworthy JSF iterating component like <ui:repeat>, <h:dataTable>, etcetera. 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. The <c:forEach> would also generate physically as much JSF components as is been iterated.

只需从id属性中省略任何EL. JSF组件将担心自动设置正确的客户机ID.此外,您可以使用<ui:repeat>varStatus属性来获取当前循环计数和索引.这样,您就不需要2个列表.计数将显示当前回合.索引是强制性的,以便通过索引获取/设置List<String>的值.因为String类没有设置器,所以<h:inputText value="#{message}" />即行不通.

Just omit any EL from id attribute. The JSF component will worry about setting the right client ID automagically. Further, you can use varStatus attribute of the <ui:repeat> to get the current loop count and index. This way you don't need 2 lists. The count will show the current round. The index is mandatory in order to get/set the value of a List<String> by index. The <h:inputText value="#{message}" /> namely won't work because the String class doesn't have a 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>

(您可以根据需要在上面的代码段中省略var="message",因为它没有在任何地方使用)

(you can if necessary omit var="message" from the above snippet as it isn't used anywhere)

这篇关于JSF2 EL未显示在组件ID中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆