如何使用 JSF 2.0 Facelets 在 XHTML 中包含另一个 XHTML? [英] How to include another XHTML in XHTML using JSF 2.0 Facelets?

查看:22
本文介绍了如何使用 JSF 2.0 Facelets 在 XHTML 中包含另一个 XHTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XHTML 页面中包含另一个 XHTML 页面的最正确方法是什么?我一直在尝试不同的方法,但都没有奏效.

What is the most correct way to include another XHTML page in an XHTML page? I have been trying different ways, none of them are working.

推荐答案

最基本的方法是.包含的内容必须放在 .

母版页的启动示例/page.xhtml:

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Include demo</title>
    </h:head>
    <h:body>
        <h1>Master page</h1>
        <p>Master page blah blah lorem ipsum</p>
        <ui:include src="/WEB-INF/include.xhtml" />
    </h:body>
</html>

包含页面 /WEB-INF/include.xhtml(是的,这是整个文件, 之外的任何标记都是不必要的因为它们无论如何都会被 Facelets 忽略):

The include page /WEB-INF/include.xhtml (yes, this is the file in its entirety, any tags outside <ui:composition> are unnecessary as they are ignored by Facelets anyway):

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h2>Include page</h2>
    <p>Include page blah blah lorem ipsum</p>
</ui:composition>
  

这个需要通过/page.xhtml打开.请注意,你不需要在里面重复 , 包含文件,否则会导致 无效的 HTML.

This needs to be opened by /page.xhtml. Do note that you don't need to repeat <html>, <h:head> and <h:body> inside the include file as that would otherwise result in invalid HTML.

您可以在 中使用动态 EL 表达式.另请参阅 如何动态 ajax-refresh通过导航菜单包含内容?(JSF SPA).

You can use a dynamic EL expression in <ui:include src>. See also How to ajax-refresh dynamic include content by navigation menu? (JSF SPA).

一种更高级的包含方式是模板.这基本上包括反过来.主模板页面应使用 声明插入定义模板内容的位置.使用主模板页面的模板客户端页面应使用 定义要插入的模板内容.

A more advanced way of including is templating. This includes basically the other way round. The master template page should use <ui:insert> to declare places to insert defined template content. The template client page which is using the master template page should use <ui:define> to define the template content which is to be inserted.

主模板页面/WEB-INF/template.xhtml(作为设计提示:页眉、菜单和页脚甚至可以依次是 文件):

Master template page /WEB-INF/template.xhtml (as a design hint: the header, menu and footer can in turn even be <ui:include> files):

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title><ui:insert name="title">Default title</ui:insert></title>
    </h:head>
    <h:body>
        <div id="header">Header</div>
        <div id="menu">Menu</div>
        <div id="content"><ui:insert name="content">Default content</ui:insert></div>
        <div id="footer">Footer</div>
    </h:body>
</html>

模板客户端页面 /page.xhtml(注意 template 属性;同样在这里,这是完整的文件):

Template client page /page.xhtml (note the template attribute; also here, this is the file in its entirety):

<ui:composition template="/WEB-INF/template.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

    <ui:define name="title">
        New page title here
    </ui:define>

    <ui:define name="content">
        <h1>New content here</h1>
        <p>Blah blah</p>
    </ui:define>
</ui:composition>

这个需要通过/page.xhtml打开.如果没有 ,则显示 中的默认内容(如果有).

This needs to be opened by /page.xhtml. If there is no <ui:define>, then the default content inside <ui:insert> will be displayed instead, if any.

您可以通过 .>

You can pass parameters to <ui:include> or <ui:composition template> by <ui:param>.

<ui:include ...>
    <ui:param name="foo" value="#{bean.foo}" />
</ui:include>

<ui:composition template="...">
    <ui:param name="foo" value="#{bean.foo}" />
    ...
</ui:composition >

在包含/模板文件中,它可以作为 #{foo} 使用.如果您需要通过many"<ui:include> 的参数,那么你最好考虑将包含文件注册为标签文件,这样你最终可以像这样使用它 何时使用 <ui:include>、标记文件、复合组件和/或自定义组件?

Inside the include/template file, it'll be available as #{foo}. In case you need to pass "many" parameters to <ui:include>, then you'd better consider registering the include file as a tagfile, so that you can ultimately use it like so <my:tagname foo="#{bean.foo}">. See also When to use <ui:include>, tag files, composite components and/or custom components?

您甚至可以通过 传递整个 bean、方法和参数.另见 JSF 2:如何将包含要调用的参数的操作传递给 Facelets 子视图(使用 ui:include 和 ui:param)?

You can even pass whole beans, methods and parameters via <ui:param>. See also JSF 2: how to pass an action including an argument to be invoked to a Facelets sub view (using ui:include and ui:param)?

不应该通过输入/猜测其 URL 来公开访问的文件,需要放置在 /WEB-INF 文件夹中,如包含文件和模板文件上面的例子.另请参阅哪些 XHTML 文件需要放在/WEB-INF 中,哪些不需要?

The files which aren't supposed to be publicly accessible by just entering/guessing its URL, need to be placed in /WEB-INF folder, like as the include file and the template file in above example. See also Which XHTML files do I need to put in /WEB-INF and which not?

之外不需要任何标记(HTML 代码).您可以输入任何内容,但 Facelets 将忽略.将标记放在那里只对网页设计师有用.另请参阅有没有办法在不构建整个项目的情况下运行 JSF 页面?

There doesn't need to be any markup (HTML code) outside <ui:composition> and <ui:define>. You can put any, but they will be ignored by Facelets. Putting markup in there is only useful for web designers. See also Is there a way to run a JSF page without building the whole project?

HTML5 文档类型是目前推荐的文档类型,尽管"它是一个 XHTML 文件.您应该将 XHTML 视为一种允许您使用基于 XML 的工具生成 HTML 输出的语言.另请参阅是否可以在 HTML 中使用 JSF+Facelets4/5?JavaServer Faces 2.2 和 HTML5 支持,为什么 XHTML 还在使用.

The HTML5 doctype is the recommended doctype these days, "in spite of" that it's a XHTML file. You should see XHTML as a language which allows you to produce HTML output using a XML based tool. See also Is it possible to use JSF+Facelets with HTML 4/5? and JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used.

CSS/JS/图像文件可以作为动态可重定位/本地化/版本化资源包含在内.另请参阅 如何在中引用 CSS/JS/图像资源Facelets 模板?

CSS/JS/image files can be included as dynamically relocatable/localized/versioned resources. See also How to reference CSS / JS / image resource in Facelets template?

您可以将 Facelets 文件放入可重用的 JAR 文件中.另请参阅具有共享代码的多个 JSF 项目的结构.

You can put Facelets files in a reusable JAR file. See also Structure for multiple JSF projects with shared code.

有关高级 Facelets 模板的真实示例,请查看 Java EE Kickoff 应用源代码OmniFaces 展示站点源代码.

For real world examples of advanced Facelets templating, check the src/main/webapp folder of Java EE Kickoff App source code and OmniFaces showcase site source code.

这篇关于如何使用 JSF 2.0 Facelets 在 XHTML 中包含另一个 XHTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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