I need to solve my problem with generating dynamic ID for some components in JSF.
Have a look at this example:
<h:outputText id="#{bean.id}" value="#{bean.value}" />
My problem is that I am getting this error:
java.lang.IllegalArgumentException: Empty id attribute is not allowed
The ID of the component is empty when I look at generated HTML output. How is this caused and how can I solve it?
Then this can happen if the
#{bean}represents the currently iterated object as declared invarattribute like so:The
id(andbinding) attribute of a JSF component is evaluated during view build time, that moment when the JSF component tree needs to be composed. However, the#{bean}is only available during view render time, that moment when<h:dataTable>needs to iterate over all objects and generate HTML table rows for each of them. The#{bean}is thus not available during view build time and evaluates tonullwhich ultimately gets EL-coerced to an empty string. And hence the exceptionjava.lang.IllegalArgumentException: Empty id attribute is not allowed.You’ve basically 3 options:
Use a view build time tag instead to iterate over a collection. You’d only need to write all HTML boilerplate yourself:
Use a plain HTML element:
Don’t set a dynamic ID, but a fixed ID. JSF will ensure of uniqueness in HTML output by prepending it with row index of the table:
See also: