Dynamically assigning IDs to tags using an EL variable

依然范特西╮ 提交于 2020-01-12 23:28:45

问题


I want to loop over a number of "guests" and insert an id dynanimally

<ui:repeat value="#{guestList}" var="guest">
    <p:inputText id="firstname_#{guest.uniqueID}" value="" label="Firstname" />
    <p:message for="firstname_#{guest.uniqueID}" />
</ui:repeat>

The problem is, that the <p:message />cannot resolve firstname_#{guest.uniqueID}

javax.faces.FacesException - Cannot find component "firstname_1" in view. at org.primefaces.component.message.MessageRenderer.encodeEnd(MessageRenderer.java:41)

It works without any problems if I write a constant inside the loop, and also I am sure that the variable can be resolved, as I can output it in that loop.

How can I instruct jsf or primefaces to resolve this variable?


回答1:


You cannot use EL expressions in the id attribute.

But you don't need to care for the uniqueness of your ids in ui:repeat. JSF does it for you. Just give your input field a "fixed" id and reference it in your p:message:

<ui:repeat value="#{guestList}" var="guest">
    <p:inputText id="firstname" value="" label="Firstname" />
    <p:message for="firstname" />
</ui:repeat>


来源:https://stackoverflow.com/questions/9770358/dynamically-assigning-ids-to-tags-using-an-el-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!