JSF模板,每个页面中都包含一段HTML [英] JSF template, include a piece of HTML in each page

查看:78
本文介绍了JSF模板,每个页面中都包含一段HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在我的Web应用程序的每个页面中包含一段HTML(例如table).

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!

推荐答案

在主模板中,您需要使用<ui:insert>声明将插入模板定义的位置.

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>

在模板客户端中,您需要使用<ui:define>定义要在模板中声明的位置插入的模板定义.

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>

不需要<ui:composition>周围的HTML.无论如何,它们将被忽略.只是不要在其中放置任何HTML,这只是浪费空间并让自己感到困惑.

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.

在浏览器中打开page.xhtml时,将在呈现的输出中显示以下内容:

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>

这篇关于JSF模板,每个页面中都包含一段HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆