简体   繁体   English

如何使用 JSF 2.0 Facelets 在 XHTML 中包含另一个 XHTML?

[英]How to include another XHTML in XHTML using JSF 2.0 Facelets?

What is the most correct way to include another XHTML page in an XHTML page?在 XHTML 页面中包含另一个 XHTML 页面的最正确方法是什么? I have been trying different ways, none of them are working.我一直在尝试不同的方法,它们都不起作用。

<ui:include>

Most basic way is <ui:include> .最基本的方法是<ui:include> The included content must be placed inside <ui:composition> .包含的内容必须放在<ui:composition>

Kickoff example of the master page /page.xhtml :母版页/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>

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

<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>
  

This needs to be opened by /page.xhtml .这需要通过/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 .请注意,您不需要在包含文件中重复<html><h:head><h:body> ,否则会导致无效的 HTML

You can use a dynamic EL expression in <ui:include src> .您可以在<ui:include src>使用动态 EL 表达式。 See also How to ajax-refresh dynamic include content by navigation menu?另请参阅如何通过导航菜单进行ajax刷新动态包含内容? (JSF SPA) . (JSF SPA)


<ui:define> / <ui:insert> <ui:define> / <ui:insert>

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.主模板页面应使用<ui:insert>声明插入已定义模板内容的位置。 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.使用主模板页面的模板客户端页面应该使用<ui:define>来定义要插入的模板内容。

Master template page /WEB-INF/template.xhtml (as a design hint: the header, menu and footer can in turn even be <ui:include> files):主模板页面/WEB-INF/template.xhtml (作为设计提示:页眉、菜单和页脚甚至可以是<ui:include>文件):

<!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>

Template client page /page.xhtml (note the template attribute; also here, this is the file in its entirety):模板客户端页面/page.xhtml (注意template属性;同样在这里,这是完整的文件):

<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>

This needs to be opened by /page.xhtml .这需要通过/page.xhtml打开。 If there is no <ui:define> , then the default content inside <ui:insert> will be displayed instead, if any.如果没有<ui:define> ,则将显示<ui:insert>的默认内容(如果有)。


<ui:param>

You can pass parameters to <ui:include> or <ui:composition template> by <ui:param> .您可以通过<ui:param>将参数传递给<ui:include><ui:composition template> <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 >

Inside the include/template file, it'll be available as #{foo} .在包含/模板文件中,它将以#{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}"> .如果您需要将“许多”参数传递给<ui:include> ,那么您最好考虑将包含文件注册为<my:tagname foo="#{bean.foo}">文件,以便您最终可以像这样使用它<my:tagname foo="#{bean.foo}"> See also When to use <ui:include>, tag files, composite components and/or custom components?另请参阅何时使用 <ui:include>、标记文件、复合组件和/或自定义组件?

You can even pass whole beans, methods and parameters via <ui:param> .您甚至可以通过<ui:param>传递整个 bean、方法和参数。 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)?另请参阅JSF 2:如何将包含要调用的参数的操作传递给 Facelets 子视图(使用 ui:include 和 ui:param)?


Design hints设计提示

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.不应该通过输入/猜测其 URL 来公开访问的文件,需要放置在/WEB-INF文件夹中,就像上面示例中的包含文件和模板文件一样。 See also Which XHTML files do I need to put in /WEB-INF and which not?另请参阅哪些 XHTML 文件需要放在 /WEB-INF 中,哪些不需要?

There doesn't need to be any markup (HTML code) outside <ui:composition> and <ui:define> . <ui:composition><ui:define>之外不需要任何标记(HTML 代码)。 You can put any, but they will be ignored by Facelets.您可以放置​​任何内容,但 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?另请参阅有没有办法在不构建整个项目的情况下运行 JSF 页面?

The HTML5 doctype is the recommended doctype these days, "in spite of" that it's a XHTML file. HTML5 doctype 是目前推荐的 doctype,“尽管”它是一个 XHTML 文件。 You should see XHTML as a language which allows you to produce HTML output using a XML based tool.您应该将 XHTML 视为一种允许您使用基于 XML 的工具生成 HTML 输出的语言。 See also Is it possible to use JSF+Facelets with HTML 4/5?另请参阅是否可以在 HTML 4/5 中使用 JSF+Facelets? and JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used .JavaServer Faces 2.2 和 HTML5 支持,为什么仍然使用 XHTML

CSS/JS/image files can be included as dynamically relocatable/localized/versioned resources. CSS/JS/图像文件可以作为动态可重定位/本地化/版本化资源包含在内。 See also How to reference CSS / JS / image resource in Facelets template?另请参阅如何在 Facelets 模板中引用 CSS/JS/图像资源?

You can put Facelets files in a reusable JAR file.您可以将 Facelets 文件放在可重用的 JAR 文件中。 See also Structure for multiple JSF projects with shared code .另请参阅具有共享代码的多个 JSF 项目的结构

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 .有关高级 Facelets 模板的真实示例,请查看Java EE Kickoff App 源代码OmniFaces 展示站点源代码src/main/webapp文件夹。

Included page:包含页面:

<!-- opening and closing tags of included page -->
<ui:composition ...>
</ui:composition>

Including page:包括页面:

<!--the inclusion line in the including page with the content-->
<ui:include src="yourFile.xhtml"/>
  • You start your included xhtml file with ui:composition as shown above.您使用ui:composition启动包含的 xhtml 文件,如上所示。
  • You include that file with ui:include in the including xhtml file as also shown above.您在包含的 xhtml 文件中使用ui:include该文件,如上所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用facelets(jsf)和xhtml进行Eclipse自动完成(内容辅助) - Eclipse autocomplete (content assist) with facelets (jsf) and xhtml 如何在jsf中将xhtml页面包含到模板中 - How to include a xhtml page into a Template in jsf 如何在不覆盖CSS的情况下在另一个XHTML中包含一个XHTML页面 - How to include an XHTML page in another XHTML without overriding the CSS 如何使用Eclipse IDE在XHTML中使用facelets开发应用程序? - How to develop application using facelets in XHTML by the Eclipse IDE? 在JSF-2.0中使用技术JSP(而不是XHTML)有什么缺点吗? - Are there any drawbacks of using technology JSP(instead XHTML) in JSF-2.0? 在使用Richfaces和Facelets开发.xhtml文件时,如何删除在Eclipse的Java构建路径中找不到的Message Ui:include? - How to remove Message Ui:include not found on java build path in eclipse, While developing .xhtml file using richfaces and facelets? JSF2.0如何用xhtml绑定托管Bean? - How JSF2.0 binds Managed Beans with xhtml? 如何使facelets符合XHTML 1.0 Transitional? - How to make facelets conform to XHTML 1.0 Transitional? 如何在IBM Rational Application Developer for Websphere 8.5中创建JSF(XHTML)facelets页面 - How to create a JSF (XHTML) facelets page in IBM Rational Application Developer for Websphere 8.5 在JSF2项目中混合使用JSP和XHTML(Facelets) - 可能吗? - Mixing JSP and XHTML (Facelets) in JSF2 Project - possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM