-1

i have a custom tag and want to allow to set the id of an inner element

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j">

<f:subview rendered="#{not empty id}">
    <h:message styleClass="message" id="#{id}" errorClass="message error"
        warnClass="message warn" for="#{element}" />
</f:subview>
<f:subview rendered="#{not id}">
    <h:message styleClass="message" errorClass="message error"
        warnClass="message warn" for="#{element}" />
</f:subview>
</html>

but i always get

Empty id attribute is not allowed

how can i archieve that when the user sets an id it is used, and when not jsf should generate it by its own

wutzebaer
  • 14,365
  • 19
  • 99
  • 170

2 Answers2

1

Use <c:if> to conditionally add a <f:attribute> with the id.

<h:message styleClass="message" errorClass="message error" warnClass="message warn" for="#{element}">
    <c:if test="#{not empty id}">
        <f:attribute name="id" value="#{id}" />
    </c:if>
</h:message>

By the way, using <html> instead of <ui:composition> in a custom tag or even without it is quite strange. Are you sure that you end up with syntactically valid HTML? Further, your second <f:subview rendered> expression is incorrect, but that has nothing to do with the particular error message you got.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Using BalusC's answer left me with IllegalArgumentException in UIComponent class where it's specifically thrown for id:

else if ("id".equals(name) || "parent".equals(name)) {
    throw new IllegalArgumentException();
}

I ended up using Omnifaces' o:tagAttribute.

semrola
  • 9
  • 4
  • How did you use the `o:tagAttribute`? If I place it as first element in the composition, then add `id="#{id}"` to the element I want it to use I get the same error of empty id not allowed if id is not explicitly set. If I place it instead of the `` inside of the element I want to use it, it does nothing. – Otto Abnormalverbraucher Dec 02 '22 at 15:25