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!

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

3 Comments

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)}
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.
Now I have do it like this: <f:event type="preRenderView" listener="#{editor.onload}"> <f:attribute name="textFieldId" value="#{includeParam}" /> </f:event> 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.