2012-03-06 65 views
6

我面臨的一個問題「在JSF中不允許使用空id屬性」,而使用下面提到的複合組件作爲一組按鈕(按鈕的數量可以是1到3)(我在Tomcat-7上使用Mojarra 2-0-8)。JSF中不允許使用空id屬性複合組件

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

有沒有更好的方法來動態生成基於計數或主頁面的任何simillar輸入的按鈕。 注意: - id,樣式和操作的名稱應該不同。

回答

5

您不能在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的一些不明顯的原因,那麼你應該創建一個標籤文件,而不是一個複合組件。