在将下面提到的复合组件用于一组按钮时,我遇到一个“JSF中不允许使用空id属性”的问题(按钮的计数可以是1到3)(我在Tomcat-7上使用Mojarra 2-0-8) 。

    <?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>

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

有没有更好的方法可以基于计数或主页面的任何类似输入来动态生成按钮。
注意:-ID,样式和 Action 的名称应不同。

最佳答案

您不能在id属性中使用渲染时间EL。给它一个固定的ID,并给组合本身也一个ID。因此,例如:

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

在实现中
<h:commandButton id="button1" ... />
<h:commandButton id="button2" ... />
<h:commandButton id="button3" ... />

然后,它们将变为foo:button1foo:button2foo:button3,从而可以在模板客户端中控制foo部分。

如果出于某种显而易见的原因确实需要动态ID,则应该创建一个标记文件,而不是复合组件。

09-30 15:33
查看更多