2012-04-01 68 views
8

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

回答

22

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

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> 
+2

简短而亲切.. !!! – kark 2013-09-16 07:49:14

相关问题