Empty id attribute is not allowed in JSF Composite component

jvG picture jvG · Mar 6, 2012 · Viewed 8.9k times · Source

I am facing an issue "Empty id attribute is not allowed in JSF" while using the below mentioned composite component for a group of buttons (button's count can be 1 to 3) (I use Mojarra 2-0-8 on Tomcat-7) .

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:composite="http://java.sun.com/jsf/composite">


    <composite:interface>       
        <composite:attribute name="buttonCount" />
        <composite:attribute name="button1Id" />
        <composite:attribute name="button1Style" />
        <composite:attribute name="button1Action" />
        <composite:attribute name="button2Id" />
        <composite:attribute name="button2Style" />
        <composite:attribute name="button2Action" />
        <composite:attribute name="button3Id" />
        <composite:attribute name="button3Style" />
        <composite:attribute name="button3Action" />

    </composite:interface>
    <composite:implementation>       
        <h:commandButton  rendered = "#{cc.attrs.buttonCount ge '1'}" id="#{cc.attrs.button1Id}" styleClass="#{cc.attrs.button1Style}">
            <f:ajax listener="#{cc.attrs.button1Action}" immediate="true"/>                                     
        </h:commandButton>
        <h:panelGroup rendered = "#{cc.attrs.buttonCount ge '2'}">
            <h:commandButton  id="#{cc.attrs.button2Id}" styleClass="#{cc.attrs.button2Style}">
                <f:ajax listener="#{cc.attrs.button2Action}" immediate="true"/>                                     
            </h:commandButton>
        </h:panelGroup> 
        <h:panelGroup rendered = "#{cc.attrs.buttonCount eq '3'}">
            <h:commandButton  id="#{cc.attrs.button3Id}" styleClass="#{cc.attrs.button3Style}">
                <f:ajax listener="#{cc.attrs.button3Action}" immediate="true"/>                                     
            </h:commandButton>
        </h:panelGroup> 
    </composite:implementation>
</html>

Use of the above CC.

<Buttons:myButton txtHeader="Title" txtDescription="text1"
                    txtAction="TextAction." button1Style="btnSave" buttonCount ="1" button1Id="btnSaveConf" button1Action="#{MyBean.save()}"></Buttons:myButton>

is there any better way to dynamically generate buttons based on a count or any simillar input form the main page. note:- id, styles and action should differ in name.

Answer

BalusC picture BalusC · Mar 6, 2012

You can't use render time EL in id attribute. Give it a fixed ID instead and give the composite itself also an ID. Thus, for example:

<buttons:myButton id="foo" ... />

with in the implementation

<h:commandButton id="button1" ... />
<h:commandButton id="button2" ... />
<h:commandButton id="button3" ... />

They will then become foo:button1, foo:button2 and foo:button3 where the foo part is thus controllable in the template client.

If you really need dynamic IDs for some unobvious reason, then you should rather create a tag file, not a composite component.