I need a method to be executed whenever my component gets rendered, i.e. when it gets created for the first time and on every update.
A way of doing it is using f:metadata and f:event for the event "preRenderComponent". It works perfectly well, unless I also have a f:metadata with f:viewParam in the view holding the component. If I have a f:viewParam in the parent view, f:event, in the component, gets ignored.
How to solve this problem, why does it happen?
Here's some code to reproduce the problem:
The view (index.xhtml):
<h:body>
<f:metadata>
<f:viewParam name="vparam" value="#{index.vparam}" />
</f:metadata>
<comp:component />
</h:body>
The managedBean (Index.java)
@ManagedBean(name="index")
public class Index implements Serializable {
private String vparam;
public void setVparam(String vparam) {
this.vparam = vparam;
}
public String getVparam() {
return vparam;
}
}
The component view (component.xhtml):
<h:body>
<composite:interface componentType="component" />
<composite:implementation>
<f:metadata>
<f:event type="preRenderComponent" listener="#{cc.generateString}" />
</f:metadata>
<h:outputText value="#{cc.string}" />
</composite:implementation>
</h:body>
The component's java class (Component.java):
@FacesComponent(value="component")
public class Component extends UINamingContainer implements Serializable {
private String string;
public void generateString(){
System.out.println("*** PRE RENDERING COMPONENT...");
System.out.flush();
string = "It worked!";
}
public String getString(){
return string;
}
}
The expected output of this code is: the string "It worked!" printed in the index page. The actual output is: nothing, since a null string is printed as "" by the h:outputText.
In the log file, "*** PRE RENDERING COMPONENT..." is not printed, it's never executed.
If I remove the following line from index.xhtml, everything works fine:
<f:viewParam name="vparam" value="#{index.vparam}" />
Why does the presence of "viewParam" in the component's parent page makes JSF to ignore "f:event" in the component?
Thanks in advance for any answer.