2

Is there a way to assign dynamic ids to h:inputHidden components?

EDIT1

I am trying to assign the ids inside a ui:repeat tag when iterating over a collection of elements.

Seitaridis
  • 4,459
  • 9
  • 53
  • 85

3 Answers3

4

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.

E.g.

<h:form id="form">
    <ui:repeat value="#{bean.list}" var="item">
        <h:inputHidden id="hidden" value="#{item.value}" />
    </ui:repeat>
</h:form>

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>

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

E.g.

<h:form id="form">
    <c:forEach items="#{bean.list}" var="item">
        <h:inputHidden id="#{item.id}" value="#{item.value}" />
    </c:forEach>
</h:form>

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>

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>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for responding. You are a legend in the world of JSF. I get an NLS missing message: CANNOT_FIND_FACELET_TAGLIB in: org.eclipse.jst.jsf.core.validation.internal.facelet.messages over the namespace declaration for c:forEach. I am using xmlns:c="http://java.sun.com/jstl/core" as namespace. – Seitaridis Apr 25 '12 at 13:51
  • For JSF2 you need `xmlns:c="http://java.sun.com/jsp/jstl/core"` instead, with the `/jsp` in the URI. – BalusC Apr 25 '12 at 13:53
  • I think you meant `` for the `ui:repeat` example. – Seitaridis Apr 25 '12 at 13:53
  • No I did absolutely not mean that. It's not possible to set the ID by `` that way. – BalusC Apr 25 '12 at 13:54
  • I get an exception: javax.faces.view.facelets.FaceletException: javax/servlet/jsp/jstl/core/LoopTagStatus ... Caused by: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.LoopTagStatus – Seitaridis Apr 25 '12 at 13:57
  • Your environment doesn't have JSTL installed. Apparently you're using a barebones servletcontainer like Tomcat or Jetty instead of a fullfledged appserver like Glassfish or JBoss AS. You'd need to install JSTL separately (like you did for JSF). Head to our `[jstl]` tag wiki page for download links and instructions: http://stackoverflow.com/tags/jstl/info Pick the JSTL 1.2 one. – BalusC Apr 25 '12 at 13:59
  • Did that, it's working. Thank you. I have another JSF question posted that is related to a datatable submit. – Seitaridis Apr 25 '12 at 14:06
1

They get assigned a dynamic ID by default. You can also specify id="#{..} to customize it.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Your answer is correct. I was searching a way to create multiple hidden input fields with ui:repeat over a collection and found that EL values are not supported for ids. So I thought that is applies all situations. – Seitaridis Apr 25 '12 at 12:09
  • I guess you can have an iteration variable and use it in the ID – Bozho Apr 25 '12 at 12:59
0

You can dynamically add any random number also (id="#{}"),but

add functionally related ids to hidden components, that will be helpful

for example if it is employee form ,you may add empid to it.

Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40