【问题标题】:Empty id attribute is not allowed in JSF Composite componentJSF Composite 组件中不允许有空的 id 属性
【发布时间】:2012-03-23 19:20:10
【问题描述】:

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

使用上述抄送。

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

有没有更好的方法可以根据计数或主页上的任何类似输入动态生成按钮。 注意:- id、styles 和 action 名称应该不同。

【问题讨论】:

    标签: jsf-2 composite-component


    【解决方案1】:

    您不能在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,那么你应该创建一个标签文件,而不是一个复合组件。

    【讨论】:

      猜你喜欢
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 2017-01-05
      • 2016-01-02
      • 2018-03-22
      相关资源
      最近更新 更多