JSF 模板,每个页面包含一段 HTML

发布于 2024-11-17 16:44:01 字数 1245 浏览 3 评论 0原文

我不知道如何在我的网络应用程序的每个页面中包含一段 HTML(比如一个小表格)。

假设这是我想要包含的表格,所以我制作了一个模板:

<?xml version ... ?>
<!DOCTYPE ...">
<html xmlns="... all the required namespaces ...">
    <head>
    </head>
    <body>
        <table>
            <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr>
        </table>
    </body>
</html>

然后我有了使用它的代码:

<?xml version ...?>
<!DOCTYPE ...">
<html xmlns="... all required namespaces ...">

    <body>
        <h3>Will this be displayed?</h3>
        <ui:composition template="tableTemplate.xhtml">
            <h4>Will this?</h4>
        </ui:composition>
    </body>

</html>

我在浏览器上看到的页面是:

<html xmlns ...>
    <head>
    </head>
    <body>
         <table>
             <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr>
         </table>
    </body>
</html>

所以有表格,但其余的都丢失了!

I can't figure out how to include a piece of HTML (say a little table) in each of the pages of my web app.

Say this is the table I want to include, so I made a template:

<?xml version ... ?>
<!DOCTYPE ...">
<html xmlns="... all the required namespaces ...">
    <head>
    </head>
    <body>
        <table>
            <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr>
        </table>
    </body>
</html>

then I have the code that uses it:

<?xml version ...?>
<!DOCTYPE ...">
<html xmlns="... all required namespaces ...">

    <body>
        <h3>Will this be displayed?</h3>
        <ui:composition template="tableTemplate.xhtml">
            <h4>Will this?</h4>
        </ui:composition>
    </body>

</html>

the page that I get on the browser is:

<html xmlns ...>
    <head>
    </head>
    <body>
         <table>
             <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr>
         </table>
    </body>
</html>

so there is the table but all the rest is missing!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-11-24 16:44:01

在主模板中,您需要使用 来声明将插入模板定义的位置。

template.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">Default title</ui:insert></title>
    </h:head>
    <h:body>
        <ui:insert name="body">Default body</ui:insert>
        <table>
            <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr>
        </table>
    </h:body>
</html>

在模板客户端中,您需要使用来定义要插入到模板中声明的位置的模板定义。

page.xhtml

<ui:composition template="template.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">
        Define your page title here
    </ui:define>

    <ui:define name="body">
        <h3>Define your body content here</h3>
        <p>Blah blah</p>
    </ui:define>
</ui:composition>

周围不需要 HTML。无论如何,他们都会被忽视。只是不要在那里放置任何 HTML,这只会浪费空间并且让您自己感到困惑。

当您在浏览器中打开 page.xhtml 时,渲染输出中将出现以下内容:

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Define your page title here</title>
    </head>
    <body>
        <h3>Define your body content here</h3>
        <p>Blah blah</p>
        <table>
            <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr>
        </table>
    </body>
</html>

In the master template, you need to use <ui:insert> to declare the place where the template definitions will be inserted.

template.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">Default title</ui:insert></title>
    </h:head>
    <h:body>
        <ui:insert name="body">Default body</ui:insert>
        <table>
            <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr>
        </table>
    </h:body>
</html>

In the template client, you need to use <ui:define> to define the template definitions which are to be inserted in the places declared in the template.

page.xhtml

<ui:composition template="template.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">
        Define your page title here
    </ui:define>

    <ui:define name="body">
        <h3>Define your body content here</h3>
        <p>Blah blah</p>
    </ui:define>
</ui:composition>

No HTML around <ui:composition> is necessary. They will be ignored anyway. Just don't put any HTML there, that's only waste of space and confusing for yourself.

When you open page.xhtml in the browser, the following will end up in the rendered output:

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Define your page title here</title>
    </head>
    <body>
        <h3>Define your body content here</h3>
        <p>Blah blah</p>
        <table>
            <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr>
        </table>
    </body>
</html>
黄昏下泛黄的笔记 2024-11-24 16:44:01

尝试

<ui:include src="tableTemplate.xhtml"/>

并在您的 tableTemplate.xhtml 中

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    put your template here

</ui:composition>

TRY

<ui:include src="tableTemplate.xhtml"/>

and in your tableTemplate.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    put your template here

</ui:composition>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文