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

Ikt*_*der 216 xhtml jsf facelets include jsf-2

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

Bal*_*usC 418

<ui:include>

最基本的方式是<ui:include>.包含的内容必须放在里面<ui:composition>.

主页的启动示例/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>
Run Code Online (Sandbox Code Playgroud)

包含页面/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>
Run Code Online (Sandbox Code Playgroud)

这需要打开/page.xhtml.请注意,您不需要重复<html>,<h:head>并且<h:body>在include文件中,否则会导致无效的HTML.

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


<ui:define>/<ui:insert>

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

主模板页面/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>
Run Code Online (Sandbox Code Playgroud)

模板客户端页面/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>
Run Code Online (Sandbox Code Playgroud)

这需要打开/page.xhtml.如果没有<ui:define>,则<ui:insert>显示内部的默认内容(如果有).


<ui:param>

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

<ui:include ...>
    <ui:param name="foo" value="#{bean.foo}" />
</ui:include>
Run Code Online (Sandbox Code Playgroud)
<ui:composition template="...">
    <ui:param name="foo" value="#{bean.foo}" />
    ...
</ui:composition >
Run Code Online (Sandbox Code Playgroud)

在include/template文件中,它将以#{foo}.如果你需要传递"many"参数<ui:include>,那么你最好考虑将include文件注册为tagfile,这样你最终可以像这样使用它<my:tagname foo="#{bean.foo}">.另请参见何时使用<ui:include>,标记文件,复合组件和/或自定义组件?

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


设计提示

通过输入/猜测其URL而不应公开访问的/WEB-INF文件需要放在文件夹中,就像上面示例中的包含文件和模板文件一样.另请参阅哪些XHTML文件需要放入/ WEB-INF,哪些不是?

这里并不需要是任何标记(HTML代码)外<ui:composition><ui:define>.你可以放任何,但Facelets 会忽略它们.将标记放在那里只对网页设计师有用.另请参见是否有办法在不构建整个项目的情况下运行JSF页面?

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

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

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

有关高级Facelets模板的真实示例,请检查Java EE Kickoff App源代码OmniFaces展示站点源代码src/main/webapp文件夹.


Ben*_*hik 23

包含的页面:

<!-- opening and closing tags of included page -->
<ui:composition ...>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)

包括页面:

<!--the inclusion line in the including page with the content-->
<ui:include src="yourFile.xhtml"/>
Run Code Online (Sandbox Code Playgroud)
  • ui:composition如上所示,您可以启动包含的xhtml文件.
  • 您将该文件包含ui:include在包含xhtml文件中,如上所示.


归档时间:

查看次数:

258680 次

最近记录:

9 年,5 月 前