如何在使用ui:composition时更改页面的头元素

Bas*_*sit 14 facelets templating jsf-2

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

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

在我的每一页上我都使用这样的东西

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

现在正在发生的事情因为ui;我的tile属性现在正在每个页面上运行,因为ui:composition会丢弃它之外的每个标签.现在在每个页面上我都有一个Login(即主模板)的标题.所以我想问一下,我怎么能这样做,在每个页面上,显示自己的标题而不是登录(主要的tempalte标题)?

谢谢

Bal*_*usC 42

在模板客户端,一切外面<ui:composition>忽略.您需要更改模板方法以<ui:insert>在主模板中提供标题,以便可以<ui:define>在模板客户端中定义它.

主模板:

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

模板客户端:

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

也可以看看:

  • 我想终于有时间在估计1000后感谢@BalusC了;)我读到的答案,这帮了很多!谢谢! (2认同)