【问题标题】:How to change head elements of a page when using ui:composition使用 ui:composition 时如何更改页面的头部元素
【发布时间】:2012-01-20 00:22:47
【问题描述】:

我想问一个问题,我有这样的主模板

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>Login</title>
    </h:head>

    <h:body>
        <div id="top">
            <ui:insert name="top">
                <ui:include src="header.xhtml" id="header"/>
            </ui:insert>
        </div>
        <div>
            <div id="content">
                <ui:insert name="content"></ui:insert>
            </div>
        </div>
        <div id="bottom" style="position: absolute;top: 675px;width: 100%" align="center">
            <ui:insert name="bottom">
                <ui:include src="footer.xhtml" id="footer"/>
            </ui:insert>
        </div>
    </h:body>
</html>

在我的每个页面上,我都在使用类似的东西

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.prime.com.tr/ui"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>City Setup</title>
    </h:head>

    <h:body>
        <ui:composition template="./WEB-INF/templates/layout.xhtml">
            <ui:define name="content">
                <h:form id="cityReviewform">
                    ......
                </h:form>

            </ui:define>
        </ui:composition>
    </h:body>
</html>

现在发生了什么,因为 ui;composition 我的 tile 属性现在正在每个页面上工作,因为 ui:composition 丢弃了它之外的每个标签。现在在每个页面上我都有一个登录标题(即主模板)。所以我想问一下,我怎样才能在每个页面上做到这一点,显示它自己的标题而不是登录名(主模板标题)?

谢谢

【问题讨论】:

    标签: jsf-2 facelets templating


    【解决方案1】:

    在模板客户端中,&lt;ui:composition&gt; 之外的所有内容都被忽略。您需要更改模板方法,为主模板中的标题提供&lt;ui:insert&gt;,以便它可以由模板客户端中的&lt;ui:define&gt; 定义。

    主模板:

    <!DOCTYPE html>
    <html lang="en"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html">
    
        <h:head>
            <title><ui:insert name="title">Login</ui:insert></title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        </h:head>
    
        <h:body>
            <div id="top">
                <ui:insert name="top">
                    <ui:include id="header" src="header.xhtml"/>
                </ui:insert>
            </div>
            <div>
                <div id="content">
                    <ui:insert name="content" />
                </div>
            </div>
            <div id="bottom">
                <ui:insert name="bottom">
                    <ui:include id="footer" src="footer.xhtml" />
                </ui:insert>
            </div>
        </h:body>
    </html>
    

    模板客户端:

    <ui:composition template="/WEB-INF/templates/layout.xhtml"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.prime.com.tr/ui"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html">
    
        <ui:define name="title">City Setup</ui:define>
    
        <ui:define name="content">
            <h:form id="cityReviewform">
                ...
            </h:form>
        </ui:define>
    </ui:composition>
    

    另见:

    【讨论】:

    • 我想在估计 1000 次之后,终于是时候感谢@BalusC 了;)我读过的答案,这很有帮助!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2021-07-27
    • 2012-02-12
    相关资源
    最近更新 更多