当使用模板时,我应该在哪里声明?

时间:2022-06-06 20:05:19

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?

我在将JSF应用程序转换为可读的页面方面取得了很大的进步,但我想知道我的方法是否正确。一个问题是,对于f:元数据标签,有一个最佳实践位置吗?

My typical Facelets client page looks like this:

我的典型Facelets客户页面是这样的:

    <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

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?

因此,f:元数据和子f:viewParam标记在我的页面中遇到。我的模式。xhtml模板还有一个部分(名为“header”),可以将这些标记放在标题部分。应该放在那里吗?这有什么不同吗?或者我是不是已经建立了一些我还没见过的副作用?

1 个解决方案

#1


39  

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()).

从技术上讲,在视图中声明 ,只要它位于顶层视图中(因此,在使用模板时,在模板客户机中,因此不在主模板中),这并不重要。在构建视图时,元数据基本不是JSF组件树的一部分,而是视图根(您可以通过ViewDeclarationLanguage#getViewMetadata()获得每个视图的基础)。

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>

#1


39  

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()).

从技术上讲,在视图中声明 ,只要它位于顶层视图中(因此,在使用模板时,在模板客户机中,因此不在主模板中),这并不重要。在构建视图时,元数据基本不是JSF组件树的一部分,而是视图根(您可以通过ViewDeclarationLanguage#getViewMetadata()获得每个视图的基础)。

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>