How to create a reuseable template with header/footer/navigation?

前端 未结 1 1999
情歌与酒
情歌与酒 2021-02-08 12:28

I have been playing with JSF and have a project working that has a header/footer/navigation/content panels. The project, however, goes from page 1 to page 2, etc., with each pag

相关标签:
1条回答
  • 2021-02-08 13:07

    This sounds like a classic case of a master template. In such a template you put everything that's common to all pages and then your actual pages reference this template and "fill in the blanks". In a way it's the reverse of the also classic include.

    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>
    

    A page uses this as follows, e.g.

    /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>
    
    0 讨论(0)
提交回复
热议问题