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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AlanObject
  • 9,613
  • 19
  • 86
  • 142

1 Answers1

44

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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. – AlanObject Mar 25 '12 at 03:27
  • 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? – Aritz Jan 20 '14 at 14:25
  • 1
    @Xtreme: Just use ``. It actually isn't required to be placed inside ``. See also http://stackoverflow.com/questions/7343220/jsf-does-it-matter-whether-place-fevent-inside-fmetadata-or-not/7343242#7343242 – BalusC Jan 20 '14 at 14:26
  • 1
    @BalusC, about placing it before ``, the same [`` tag documentation](http://docs.oracle.com/javaee/7/javaserverfaces/2.2/vdldocs/facelets/f/metadata.html) says: `This must be a child of the `. Is there an implicit `f:view` at the top level or something? – Vsevolod Golovanov Feb 07 '14 at 14:18
  • 2
    @this: that's correct. It's the `UIViewRoot`. See also http://stackoverflow.com/q/8883476 – BalusC Feb 07 '14 at 14:24