转发模板参数和定义

时间:2012-08-07 13:58:11

标签: jsf facelets

考虑一个嵌套模板:

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>

是否必须通过重新定义,将<ui:param>中的<ui:define>Layout.xhtml转发到Base.xhtml模板。例如:

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

还可以将此问题改为:“模板参数是否表现为级联?”

1 个答案:

答案 0 :(得分: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