2012-08-07 28 views
1

考慮嵌套模板:轉發模板參數和定義

Base.xhtml:

... 

<h:outputText value="#{uiParamter}"/> 
<ui:insert name="header"/> 

... etc. 

Layout.xhtml:

<ui:composition template="Base.xhtml"> 

    ... 

    <ui:insert name="content"/> 

    ... etc. 

</ui:composition> 

當現在定義像模板客戶端這個:

<ui:composition template="Layout.xhtml"> 

    <ui:define name="header"> foo </ui:define> 
    <ui:define name="content"> foo2 </ui:define> 
    <ui:param name="uiParameter" value="foo3"/> 

</ui:composition> 

是否必須通過重新定義模板,將Layout.xhtml中的<ui:param><ui:define>轉換爲Base.xhtml模板。 e.g:

<ui:param name="uiParameter" value="#{uiParameter}"> 
<ui:define name="header"> 
    <ui:insert name="header"/> 
</ui:define> 

人們還可以改寫這個問題爲:「做模板參數表現得級聯?」

回答

4

答案是肯定的。我已經運行在JBoss下面的代碼AS 7.0:

nest1.xhtml:

<!DOCTYPE html> 
    <html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:ui="http://java.sun.com/jsf/facelets"> 

    <h:head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <title><ui:insert name="title"> 
       Nested 
      </ui:insert></title> 
    </h:head> 

    <h:body> 
     <ui:insert name="main" /> 
     <h:outputText value="#{uiParam}"/> 
    </h:body> 

</html> 

nest2.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" template="nest1.xhtml"> 

    <ui:define name="main"> 
     <p>Nested templated content.</p> 
    </ui:define> 
</ui:composition> 

nest3.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" template="nest2.xhtml"> 

    <ui:define name="title">Nested Template</ui:define> 
    <ui:param name="uiParam" value="ui param value" /> 
</ui:composition> 

它被渲染爲:

screenshot of jboss output