When using <ui:composition> templating, where should I declare the <f:metadata>?
Asked Answered
F

1

25

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?

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

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?

Fenske answered 25/3, 2012 at 0:12 Comment(0)
H
45

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

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>
Halmahera answered 25/3, 2012 at 1:25 Comment(5)
I must say that looks a bit cleaner than what I have been doing. Somehow it didn't occur to me to create a ui:insert just for metadata purposes, but I will now.Fenske
Does it mean f:metadata can't be placed for example in a page template in order to make it reusable? What to do then if there's some action we want to execute for every view before they get rendered?Nealson
@Xtreme: Just use <f:event>. It actually isn't required to be placed inside <f:metadata>. See also #7343720Halmahera
@BalusC, about placing it before <h:head>, the same <f:metadata> tag documentation says: This must be a child of the <f:view>. Is there an implicit f:view at the top level or something?Gesellschaft
@this: that's correct. It's the UIViewRoot. See also https://mcmap.net/q/336175/-when-to-use-f-view-and-f-subviewHalmahera

© 2022 - 2024 — McMap. All rights reserved.