2011-03-13 69 views
21

讓JSF 1.2兩頁(one.xhtml和other.xhtml),
由以下規則包含到當前頁面:JSF 1.2:界面:包括與參數

... 
    <c:if test="#{flowScope.Bean.param1}"> 
     <ui:include src="one.xhtml"/> 
    </c:if> 

    <c:if test="#{!flowScope.Bean.param1}"> 
     <ui:include src="other.xhtml"/> 
    </c:if> 
... 

one.xhtml不同於other.xhtml只能通過動作參數:

one.xhtml:<h:commandLink action="actionOne">
other.xhtml:<h:commandLink action="actionTwo">

是否有可能使用一些一般XHTML?
代替one.xhtml和other.xhtml,這樣的事情:

... 
    <c:if test="#{flowScope.Bean.param1}"> 
     <ui:include src="general.xhtml" param="actionOne"/> 
    </c:if> 

    <c:if test="#{!flowScope.Bean.param1}"> 
     <ui:include src="general.xhtml" param="actionTwo"/> 
    </c:if> 
... 

謝謝你的幫助。

回答

47

您需要在<ui:include>之內嵌套<ui:param>以將參數傳遞到包含的文件。

<ui:include src="general.xhtml"> 
    <ui:param name="action" value="actionOne" /> 
</ui:include> 

,並在包括:

<h:commandButton action="#{action}" /> 

注意,這僅支持字符串,而不是操作方法。對於後者,您需要升級到JSF 2.0並使用composite components

+0

感謝,我KHOW約CC,他們是偉大的,但在目前的jsf 1.2項目不能使用它們。我會嘗試你的解決方案並寫回結果。 – sergionni 2011-03-13 17:53:05

+0

它將基於您在問題中提出的方式工作。但是,如果您使用'#{bean.doSomething}'而不是'actionOne',那麼您確實需要獲取JSF 2.0複合組件。 – BalusC 2011-03-13 18:04:55

+0

奇怪的是,它拋出:action =「#{action}」:身份'action'爲空,無法調用 – sergionni 2011-03-13 18:06:33

19

除了BalusC的回答是:

注意,這僅支持字符串, 不採取行動的方法。對於後者,您需要將 升級到JSF 2.0和 使用複合組件。

有一種方法與JSF 1.2要做到這一點,雖然它有點難看:

<ui:include src="general.xhtml"> 
    <ui:param name="actionBean" value="#{myBackingBean}" /> 
    <ui:param name="actionMethod" value="edit" /> 
</ui:include> 

<h:commandButton action="#{actionBean[actionMethod]}" />