5

I have a view that is included in another view. I'm running into problems with f:metadata in the included view. Not sure how to go about it. I've updated both views and restarted the server. No luck.

Parent view

<ui:include src="sub_entityIndexPagination.xhtml">
    <ui:param name="entityIndexBean" value="#{articleIndexBean}"/>
</ui:include>

Sub view

<ui:composition 
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:c="http://java.sun.com/jsp/jstl/core"      
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  >

<f:metadata>
    <f:viewParam name="pn" value="#{entityIndexBean.currentEntityPageNumber}" />
     <f:event type="preRenderView" listener="#{entityIndexBean.toCurrentEntityPage()}" />
</f:metadata>

<c:forEach var="pageNumber" begin="1" end="${entityIndexBean.getEntityPageCount()}">   
    <h:outputLink value="ar_index.xhtml">
        <h:outputText value="${pageNumber}" />
            <f:param name="pn" value="${pageNumber}" />
    </h:outputLink> 
</c:forEach>

JSF Implementation

c:\tomee16\lib\myfaces-api-2.1.13.jar

Error

/sub_entityIndexPagination.xhtml at line 9 and column 14 <f:metadata> Parent UIComponent j_id_9 should be instance of UIViewRoot

phaseId=RENDER_RESPONSE(6)

Caused by:
javax.faces.view.facelets.TagException - /sub_entityIndexPagination.xhtml at line 9 and column 14 <f:metadata> Parent UIComponent j_id_9 should be instance of UIViewRoot
at org.apache.myfaces.view.facelets.tag.jsf.core.ViewMetadataHandler.apply(ViewMetadataHandler.java:60)
jacekn
  • 1,521
  • 5
  • 29
  • 50

2 Answers2

7

The <f:metadata> can't be declared in an include file. It has to be declared in the top view (the file which you indicated as "parent view"). It should be exactly that view which is directly referenced by the request URL.

That said, it's completely wrong to have a <html> in an include file. You're this way effectively nesting <html> tags which only results in syntactically invalid HTML. It would probably be worth the effort to take a JSF pause and learn some basic HTML first. JSF is ultimately just a HTML code generator.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • #1: if you elaborate the functional requirement, you may get the right answer. #2: http://stackoverflow.com/questions/4792862/how-to-include-another-xhtml-in-xhtml-using-jsf-2-0-facelets/ Still, it's really important to learn and understand some basic HTTP and HTML before diving into JSF. Else it will bite you because you don't understand why exactly it does this and that. – BalusC Feb 15 '14 at 22:43
4

Another reason this might show up is if there is any tag, including , between <ui:composition> and <f:metadata> in facelets.

e.g:

Good:

<ui:composition 
...
template="..."> 
<f:metadata>
    <f:viewParam ...

Also OK, in case you need to set params on f:view:

<ui:composition 
...
template="..."> 
<f:view ...params goes here... /> <!-- ends here, does not enclose anything -->
<f:metadata>
    <f:viewParam ...

Bad:

<ui:composition 
...
template="...">
<f:view>  <!-- you don't even need this with facelets unless you want to pass in parameters. --> 
<f:metadata>
    <f:viewParam ...
</f:view> <!--f:view encloses f:metadata so f:metadata is no longer a direct  child of UIViewRoot. -->

Full explanation from Jakob Korherr is available here: https://mail-archives.apache.org/mod_mbox/myfaces-users/201104.mbox/%3cBANLkTikT41optf6=ZLpPQgWCJjGhkuAYsQ@mail.gmail.com%3e

Erik I
  • 972
  • 1
  • 11
  • 28