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



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

这听起来像是主模板的经典案例。在这样一个模板中,你把所有页面通用的东西都放进去,然后你的实际页面引用这个模板并"填空"。在某种程度上,它与同样经典的include相反。

例如

/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>

最新更新