Search code examples
jsfjsf-2.2faceletsfacelet-tag

How to use a default generated ID in a Facelet tag


I've created a custom Facelet tag. I would like the ID to fall back to the default j_idtXxx just like if you would have left out the entire ID attribute when it is not set in the XHTML.

So, I would like <my:tag id="fiets"/> to be rendered as <span id="fiets"/>.

But, <my:tag/> should be rendered as <span id="j_idtXxx"/>.

If I would use <h:anyTag id="#{id}"/> in my tag file, it simply fails on the empty ID attribute. Is there any way to be rendered as a default generated ID? I would like to create something like:

<c:set var="id" value="#{empty id ? USE_DEFAULT : id}" />

But I don't know what to use at USE_DEFAULT.


Solution

  • The functionality is available by UIViewRoot#createUniqueId(). The current UIViewRoot instance is in EL available as implicit object #{view}.

    So, this should do:

    <c:set var="id" value="#{empty id ? view.createUniqueId() : id}" />