如何包含多个xhtml页面,这些页面将相同的模板扩展到摘要xhtml页面,

时间:2012-12-27 12:33:38

标签: jsf jsf-2 facelets

我们如何将多个xhtml页面包含在摘要页面中。 这里所有的xhtml页面都包含相同的模板。

commonTemplate.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:c="http://java.sun.com/jsp/jstl/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<title> SNS </title>
<meta http-equiv="expires" content="0"/>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="sns.css" type="text/css" />
</head>
<h:body>

    <div id="header">
        <ui:insert name="commonHeader">
            <ui:include src="header.xhtml" />
        </ui:insert>
    </div>
    <div id="content">
        <ui:insert name="commonBodyContent">
            Common Body Content.
        </ui:insert>
    </div>
    <div id="footer">
        <ui:insert name="commonFooter">
            <ui:include src="footer.xhtml" />
        </ui:insert>
    </div>
</h:body>
</html>

updatePersonalDetails.xhtml

<ui:composition 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"
                template="commonTemplate.xhtml">

    <ui:define name="commonBodyContent">
        .........;
        ..........;
    </ui:define>

</ui:composition>

updatedAddress.xhtml

<ui:composition 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"
                template="commonTemplate.xhtml">

    <ui:define name="commonBodyContent">
        .........;
        ..........;
    </ui:define>

</ui:composition>   

selectPreferences.xhtml

<ui:composition 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"
                template="commonTemplate.xhtml">

    <ui:define name="commonBodyContent">
        .........;
        ..........;
    </ui:define>

</ui:composition>

summary.xhtml

<ui:composition 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:include src="updatePersonalDetails.xhtml" />
    <ui:include src="updatedAddress.xhtml" />
    <ui:include src="selectPreferences.xhtml" />

</ui:composition>   

无论我在所有xhtml页面中拥有什么数据,都应该在摘要页面中显示完全相同。但是包括这会导致在页面上呈现多个<html>文档。

我们如何解决这个问题?

1 个答案:

答案 0 :(得分:7)

将正文内容移动到模板客户端中<ui:include>所包含的另一个模板中。

E.g。 updatePersonalDetails.xhtml

<ui:composition 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"
                template="commonTemplate.xhtml">

    <ui:define name="commonBodyContent">
        <ui:include src="updatePersonalDetails-content.xhtml" />
    </ui:define>

</ui:composition>

(也为其他人重复)

这样您就可以在summary.xhtml中执行此操作:

<ui:composition 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"
                template="commonTemplate.xhtml">

    <ui:define name="commonBodyContent">
        <ui:include src="updatePersonalDetails-content.xhtml" />
        <ui:include src="updatedAddress-content.xhtml" />
        <ui:include src="selectPreferences-content.xhtml" />
    </ui:define>

</ui:composition>   

对于具体问题

无关,请考虑在/WEB-INF文件夹中放置模板和包含,以防止直接访问它们。另请参阅Which XHTML files do I need to put in /WEB-INF and which not?