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

查看:129
本文介绍了如何使用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.

推荐答案

< ui:include>



最基本的方法是 < ui:include> 。包含的内容必须放在< UI:组合物>

主页的开球示例 /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 (是的,这是完整的文件,< ui:composition> 之外的任何标签都是不必要的,因为它们会被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> < h:head> 和<$包含文件中的c $ c>< h:body> 否则会导致 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.

您可以在< ui:include src> 中使用动态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).

更高级的包含方法是模板。这基本上包括另一种方式。主模板页面应使用 < ui:insert> 以声明插入已定义模板内容的位置。使用主模板页面的模板客户端页面应使用 < ui:define> 定义要插入的模板内容。

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 (作为设计提示:标题,菜单和页脚甚至可以< ui:include> 文件):

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 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 。如果没有< ui:define> ,那么< ui:insert> 中的默认内容将是显示相反,如果有的话。

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.

您可以将参数传递给< ui:include> < ui:composition template> < ui:param>

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> ,那么您最好考虑将包含文件注册为标记文件,这样您最终可以像这样使用< my:tagname foo =#{bean.foo}> 。另请参阅何时使用< 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?

您甚至可以通过< ui:param> 传递整个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?

< ui:composition> < ui:之外不需要任何标记(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 doctype是目前推荐的doctype,尽管它是一个XHTML文件。您应该将XHTML视为一种允许您使用基于XML的工具生成HTML输出的语言。另请参见是否可以在HTML中使用JSF + Facelets 4/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模板的世界示例,检查 src / main / webapp 文件夹apprel =noreferrer> Java EE Kickoff App源代码 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天全站免登陆