Expression Language (EL) not working for id tag

Wondering why assigning a variable to an id tag does not work, although you can assign the variable to the for or value attribute without any problems?

The reason is that expression evaluation on the id attribute is explicitly disallowed (maybe for some security reasons). So you cannot generate dynamic ids using EL as in the code below.

<ui:repeat value="#{bean.elements}" var="e">
<h:outputlabel value="#{e.label}" for="#{}">
<h:inputtext id="#{}" value="#{bean[e.variable]}">

You will likely get an Illegal Argument Exception.

But you can simply write the code as follows, JSF will automatically assign an iteration number to the id myid and the final id will be the same for outputLabel and inputText.

<ui:repeat value="#{bean.elements}" var="e">
<h:outputlabel value="#{e.label}" for="myid">
<h:inputtext id="myid" value="#{bean[e.variable]}">

You can verify this by checking the ids in FireBug.

Firebug results:

<label for="bean:j_id53:0:myid" ...="">
<input id="bean:j_id53:0:myid" ...="">
<label for="bean:j_id53:1:myid" ...="">
<input id="bean:j_id53:1:myid" ...="">
<label for="bean:j_id53:2:myid" ...="">
<input id="bean:j_id53:2:myid" ...="">

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.