0

I have the following part of a .xhtml page:

<ui:composition template="./templates/template.xhtml">
<ui:define name="mainContent">

    <ui:include src="include/includeAbleEditor.xhtml">
        <ui:param name="includeParam" value="MyClass" />

    </ui:include>



    <ui:include src="include/includeAbleEditor.xhtml">
        <ui:param name="includeParam" value="YourClass" />
    </ui:include>

</ui:define>

In the "includeAbleEditor.xhtml" I want to call a method after it was included (In this case this should happend two times).

Now I tried to solve it like this: (metadata tag is part of the includeAbleEditor.xhtml)

<f:metadata>
    <f:event type="preRenderView" listener="#{editor.onload}" />
    <f:attribute name="textFieldId" value="#{includeParam}" />
</f:metadata>

The Problem:

The method is being called only once. But it should be called two times. Once with the parameter "MyClass" and once with "YourClass".

Do you have any suggestions?

Thanks a lot!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mansouritta
  • 166
  • 1
  • 1
  • 10

1 Answers1

3

There can be only one <f:metadata> in the entire view and it must be in the top level view. Unlike e.g. <f:view>, they don't "extend" each other and all others will be ignored.

You actually don't need it here. It's only necessary whenever you need to attach <f:viewParam> and/or <f:viewAction> to the specific view. The <f:event> doesn't require a <f:metadata>. It will just be hooked to the parent UIComponent. It was during JSF 2.0/2.1 ages (when <f:viewAction> didn't exist) being abused to have a hook to invoke a listener after <f:viewParam> values are being set. It's just for self-documentary purposes being placed in the same <f:metadata> as where all <f:viewParam>s are.

So, just get rid of it.

<f:event type="preRenderView" listener="#{editor.onload(includeParam)}" />

That said, postAddToView is likely a better event to hook this all on. And to avoid "Duplicate component ID" errors over all place later on, consider wrapping it in <f:subview> or making it a composite.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • there is an error.... Caused by: javax.faces.view.facelets.TagAttributeException: /include/includeAbleEditor.xhtml 58,76 listener="#{editor.onload(includeParam)}" Error Parsing: #{editor.onload(includeParam)} – Mansouritta Jun 04 '15 at 13:20
  • So, you're still on legacy Servlet 2.5 or so? Didn't expect that these days. Well, if you can't upgrade to Servlet 3.0+ (released more than 5 years ago already), stick to `f:attribute` workaround then. This "error" is not further related to the concrete problem. – BalusC Jun 04 '15 at 13:21
  • Now I have do it like this: here's a snip of the method "onload" : String id = (String) event.getComponent().getAttributes().get("textFieldId"); But the id is null. The event object isn't null! weird – Mansouritta Jun 04 '15 at 13:47