2014-01-28 44 views
1

我構建了一個複合組件,它接受一個輸入ID並生成一個表。然後,我做它接受使用<f:viewParam> GET參數的網頁喜歡這樣的:如何將GET參數傳遞給複合組件?

<f:metadata> 
     <f:viewParam name="id" value="#{aBean.id}" /> 
</f:metadata> 
<util:aCompositeComponent inputId="#{aBean.id}" /> 

的複合材料部件的標記是:

<cc:interface componentType="compositeComponentBacking"> 
    <cc:attribute name="inputId" type="java.lang.Integer" /> 
</cc:interface> 

<cc:implementation>   
     /** 
      Use #{cc.result} to build a HTML table to present a result 
     **/ 
</cc:implementation> 

和複合組件的支持bean是:

@FacesComponent("compositeComponentBacking") 
public class CompositeComponentBacking extends UINamingContainer { 

    private Integer inputId; 

    public List<Result> getResult() { 

     //Use inputId to query service to return result 
     return result; 
    } 

    public Integer getInputId() { 
     return inputId; 
    } 

    public void getInputId(Integer inputId) { 
     this.inputId = inputId; 
    } 

} 

GET參數可以綁定到#{aBean.id},但#{aBean.id}不能傳遞到複合組件,並且它始終在第e複合組件。我如何將GET參數傳遞給複合組件?


更新:

我終於解決.I發現,使用複合材料部件時,如果輸入屬性爲恆定(例如<util:aCompositeComponent inputId="1"/>),這個常數可以設置爲CompositeComponentBacking的問題inputId字段。但如果我使用輸入屬性的複合是一個EL表達式(例如<util:aCompositeComponent inputId="#{aBean.id}"/>,EL表達式的值不能設置爲CompositeComponentBackinginputId字段。我必須通過在CompositeComponentBacking內部通過計算#{cc.attrs.inputId}來獲得該值。正常的行爲?

+0

當您在頁面(組件外部)輸出'#{aBean.id}'時,是否設置了值? – mabi

+0

是的。 #{aBean.id}正確顯示,如果我把它放在組件旁邊,但在組合組件內部,它是空的。 –

+2

顯然你在合成內部訪問/顯示它是錯誤的。很難說,如果你沒有在問題中表現出這一部分。 – BalusC

回答

3

的Attr表示值表達式的ibute值通過setValueExpression("attributeName", valueExpression)設置,而不是通過setAttributeName(evaluatedValue)設置。你知道,JSF/Facelets EL被推遲,而不是隱含的。

你應該在getResult()基本上被getAttributes()圖獲得的評估值:

public List<Result> getResult() { 
    Integer inputId = (Integer) getAttributes().get("inputId"); 
    // ... 
} 

如果你真的想添加一個getter和setter,然後委託給getStateHelper()

public Integer getInputId() { 
    return (Integer) getStateHelper().eval("inputId"); 
} 

public void setInputId(Integer inputId) { 
    getStateHelper().put("inputId", inputId); 
} 

(注意:不要混合使用它們; getAttributes().get()將掃描的getter,然後再調用它;如果你使用getAttributes().get()吸氣劑本身裏面,它會調用自身無限循環導致錯誤由本網站名稱代表)

0

如果你一定的價值是存在的,你可以使用

<util:aComposite inputId="#{param.id}" /> 

訪問請求參數id,它無法通過驗證,轉換或什麼的。

+0

已經嘗試,但仍然相同。 GET參數不能傳遞給複合組件 –