2

In the following page i am not able to load automobileLists since the method to populate it is outside the component in a f:metadata. I have a nullPointerException error. Partial code :

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>title</title>
</h:head>
<f:metadata>
    <f:viewAction action="#{primeAutomobileController.populateAutomobileFieldList}"/>
</f:metadata>

<ui:composition template="layout/template.xhtml">
    <ui:define name="content">.....................

The only way for me to load it is to scope the primeAutomobileController to session instead of the original request, and call the method from a preceding page through a button, i would like it to load at the starting of the page instead withouth calling it previously. The method in question :

public void populateAutomobileFieldList(){
    List<String> automobileFieldSource = new ArrayList<>();
    List<String> automobileFieldTarget = new ArrayList<>();
    automobileFieldSource.add("Make");
    automobileFieldSource.add("Model");
    automobileFieldSource.add("Year");
    automobileFieldSource.add("Description");
    setAutomobileList(new DualListModel<>
        (automobileFieldSource, automobileFieldTarget));
}

Partial index.xhtml page where f:metadata gets loaded

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>title</title>
</h:head>
<f:metadata>
    <f:viewAction action="#{primeAutomobileController.loadAutomobiles}"/>
    <f:viewAction action="#{primeAutomobileController.populateAutomobileFieldList}"/>
</f:metadata>

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

    <ui:define name="content"> ......................

Here both methods in f:metadata get properly loaded, like it's shown in an example in a video tutorial i'm following, but when it's the same exact code in a differe xhtml it doesn't work.

MrSir
  • 576
  • 2
  • 11
  • 29

1 Answers1

2

The documentation of metadata tag shows how it must be done when using templating (how the template must look like and how to use it in the template client) :

The implementation must allow templating for this element according to the following pattern.

template client XHTML view, view01.xhtml

<ui:composition template="template.xhtml">
    <ui:define name="metadata">
      <f:metadata>
        <f:viewParam name="id"/>
      </f:metadata>
    </ui:define>
    <ui:define name="content">
        <h1>The big news stories of the day</h1>
    </ui:define>
</ui:composition>

Note line 4. The page author must ensure that the <f:metadata> element does not appear on a template or included page. It must reside on the root page that corresponds to the viewId.

The template page, template.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xml:lang="en" lang="en">

<body>
<f:view>

        <ui:insert name="metadata"/>

    <div id="container">
        <ui:insert name="content"/>
    </div>
</f:view>
</body>
</html>

The page author is not required to use templating, but if they do, it must be done as shown above

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
krokodilko
  • 35,300
  • 7
  • 55
  • 79
  • I've tried following the example but it still doesn't load to me, i added in the post my `index.xhtml` page, where the exact same code gets loaded. – MrSir Sep 30 '17 at 10:16
  • @mrsir: did you actually read the answer and compare the answer with your code? – Kukeltje Sep 30 '17 at 11:49