使用 <ui:composition> 时模板,我应该在哪里声明 &lt;f:metadata&gt;? [英] When using &lt;ui:composition&gt; templating, where should I declare the &lt;f:metadata&gt;?

查看:23
本文介绍了使用 <ui:composition> 时模板,我应该在哪里声明 &lt;f:metadata&gt;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 JSF 应用程序转换为可标记的页面方面取得了很大进展,但我想知道我的做法是否正确.一个问题是 f:metadata 标签是否有最佳实践位置?

I have made a lot of progress in converting my JSF applications to book-markable pages, but I am wondering if I am doing it the right way. One question is that is there a best-practice location for the f:metadata tags?

我典型的 Facelets 客户端页面如下所示:

My typical Facelets client page looks like this:

    <ui:composition template="./pattern.xhtml">

        <ui:define name="content">

            <f:metadata>
                <f:viewParam name="userId" value="#{bean.userId}" />
                <f:viewParam name="startRecord" value="#{bean.startRecord}" />
                <f:viewParam name="pageSize" value="#{bean.pageSize}" />
                <f:viewParam name="sort" value="#{bean.sort}" />
            </f:metadata>

            <h1>Data Table</h1>

etc

因此 f:metadata 和子 f:viewParam 标记在我的页面正文中遇到.我的 pattern.xhtml 模板还有一个部分(名为header"),可以将这些标签放在 header 部分中.他们应该放在那里吗?它有什么不同,还是我已经准备好了一些我还没有看到的副作用?

So the f:metadata and child f:viewParam tags are encountered in the body of my page. My pattern.xhtml template also has a section (named "header") that could put these tags in the header section. Should they be put there? Does it make a difference or am I set up for some side effect I haven't seen yet?

推荐答案

从技术上讲,在视图中声明 的位置并不重要,只要它在顶级视图(因此,当使用模板时,在模板客户端中,因此不在主模板中).构建视图时,元数据基本上不是 JSF 组件树的一部分,而是视图根的一部分(您可以通过 ViewDeclarationLanguage#getViewMetadata()).

Technically, it doesn't matter where you declare the <f:metadata> in the view as long as it's in the top level view (so, when using templating, in the template client and thus not in the master template). When the view get built, the metadata is basically not part of the JSF component tree, but of the view root (which you can obtain on a per-view basis by ViewDeclarationLanguage#getViewMetadata()).

大多数自文档化是将 <f:metadata> 放在视图的顶部,这样您就可以第一眼看到任何元数据,而无需滚动到一半或底部视图源代码.

Most self-documenting would be to put the <f:metadata> in the top of the view, so that you can see any metadata at first glance without the need to scroll to halfway or bottom the view source code.

使用普通页面时,只需将其放在 之前即可.

When using a plain page, just put it right before the <h:head>.

<!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"
>
    <f:metadata>
        <f:viewParam name="userId" value="#{bean.userId}" />
        <f:viewParam name="startRecord" value="#{bean.startRecord}" />
        <f:viewParam name="pageSize" value="#{bean.pageSize}" />
        <f:viewParam name="sort" value="#{bean.sort}" />
    </f:metadata>

    <h:head>
        ...
    </h:head>

    <h:body>
        ...
    </h:body>
</html>

使用模板时,推荐的方法,如中所述 标签文档,将在主模板中声明一个单独的 并让客户端在 中定义 .

When using templating, the recommended approach, as stated in the <f:metadata> tag documentation, would be to declare a separate <ui:insert name="metadata"> in the master template and let the client define the <f:metadata> in an <ui:define name="metadata">.

<ui:composition template="/WEB-INF/pattern.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="metadata">
        <f:metadata>
            <f:viewParam name="userId" value="#{bean.userId}" />
            <f:viewParam name="startRecord" value="#{bean.startRecord}" />
            <f:viewParam name="pageSize" value="#{bean.pageSize}" />
            <f:viewParam name="sort" value="#{bean.sort}" />
        </f:metadata>
    </ui:define>

    <ui:define name="content">
        <h1>Data Table</h1>
        ...
    </ui:define>
</ui:composition>

这篇关于使用 <ui:composition> 时模板,我应该在哪里声明 &lt;f:metadata&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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