【问题标题】:Unable to replace or define a named ui:insert block无法替换或定义命名的 ui:insert 块
【发布时间】:2016-11-16 09:53:40
【问题描述】:

我刚刚了解到我可以创建一个主模板,我可以使用ui:insertui:defineui:include src将其替换为不同的其他xhtml 页面,所以我在网上做了一些研究并尝试显示合并页面。

但是,我无法用另一个内容定义命名的 ui:insert content(我想在用户进入注册页面时输入 register.xhtml 页面的内容)

template.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
    <h:head>
        <h:outputStylesheet name="css/bootstrap.css" />
        <h:outputStylesheet name="css/font-awesome.css"/>
        <h:outputStylesheet name="css/mywebsite.css"/>
        <h:outputScript name="js/jquery-3.1.1.js"/>
        <h:outputScript name="js/bootstrap.js"/>
    </h:head>    

    <h:body>

        <div id="page">
            <div id="header">
                <ui:insert name="header">
                    <ui:include src="header.xhtml"/>
                </ui:insert>
            </div>

            <div id="content">
                <!--content container-->
                <ui:insert name="content">
                    <ui:include src="content.xhtml" />
                </ui:insert>
            </div>

            <div id="footer">
                <!--footer container-->
                <ui:insert name="footer">
                    <ui:include src="footer.xhtml" />
                </ui:insert>
            </div>
        </div>

    </h:body>

</html>

header.xhtml

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h1>Default Header</h1>
</ui:composition>

content.xhtml

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    >
    <h1>Default Content</h1>
</ui:composition>

footer.xhtml

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h1>Default Footer</h1>
</ui:composition>

register.xhtml

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h1>I AM REGISTER</h1>
</ui:composition>

当用户转到 localhost:8080/mywebsite/register.xhtml 时,我希望能够将 content 块替换为 register.xhtml

我想替换 default content div。仅当用户进入 注册 页面 (localhost:8080/mywebsite/register.xhtml) 但保留 headerfooter

希望你能帮忙。

谢谢。

【问题讨论】:

    标签: jsf


    【解决方案1】:

    你的 register.xhtml 应该是这样的:

    <ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    template="/WEB-INF/templates/template.xhtml">
    <ui:define name="content">
        <h1>I AM REGISTER</h1>
    </ui:define></ui:composition>
    

    您必须在ui:composition 标签中添加template="/WEB-INF/templates/template.xhtml",并将实际内容覆盖为

    <ui:define name="content">
        <h1>I AM REGISTER</h1>
    </ui:define>
    

    【讨论】:

    • 谢谢。我确实这样做了,但第一次没有工作。我发现浏览器错误地将其解释为 xml。我发现我必须将映射 url-patter 从 . 更改为 .xhtml Faces Servlet.xhtml
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2020-03-30
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多