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

use*_*928 8 jsf templates facelets jsf-2

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

Arj*_*jms 23

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

例如

/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>
Run Code Online (Sandbox Code Playgroud)

页面使用如下,例如

/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>
Run Code Online (Sandbox Code Playgroud)