如何使用页眉/页脚/导航创建可重用的模板?

时间:2012-04-01 15:20:10

标签: templates jsf jsf-2 facelets

我一直在玩JSF并且有一个项目正在运行,它有一个页眉/页脚/导航/内容 面板。然而,该项目从第1页到第2页等,每个页面具有不同的布局。如何创建一个可重复使用的模板,在页面之间保持相同的外观,即页眉/页脚/导航保持不变,但内容是否更新?

1 个答案:

答案 0 :(得分:23)

这听起来像是主模板的经典案例。在这样的模板中,您可以将所有页面的所有内容放在一起,然后您的实际页面会引用此模板并“填写空白”。在某种程度上,它也是经典的反面。

E.g。

<强> /WEB-INF/templates/masterTemplate.xhtml:

<!DOCTYPE html>
<html lang="en"
    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" 
>
    <h:head>
        <title>
            <ui:insert name="title">Some title</ui:insert>
        </title>        
    </h:head>

    <ui:include src="header.xhtml"/>

    <h:body>
        <ui:insert name="content" />
    </h:body>

    <ui:include src="footer.xhtml"/>

</html>

页面使用如下,例如

<强> /hello.xhtml

<ui:composition template="/WEB-INF/templates/masterTemplate.xhtml"
    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" 
>
   <ui:define name="title">hello</ui:define>

    <ui:define name="content">
        Hi, this is the page
    </ui:define>
</ui:composition>
相关问题