Empty id attribute not allowed in JSF Composite component

I ran into a problem: "An empty id attribute is not allowed in JSF" when using the component below for a group of buttons (the number of buttons can be from 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 a better way to dynamically generate buttons based on a counter or any similary input on the main page. note: - id, styles and actions must differ in name.

+6
source share
1 answer

You cannot use EL rendering time in id attribute. Give it a fixed identifier and give the composite itself also an identifier. Thus, for example:

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

c in implementation

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

Then they will become foo:button1 , foo:button2 and foo:button3 , where the part of foo is thus controlled by the template client.

If you really need dynamic identifiers for some non-obvious reason, you'd better create a tag file rather than a composite component.

+5
source

Source: https://habr.com/ru/post/910062/


All Articles