JSF Converter seems like is invoked before any other managed bean on the xhtml page. It is even created with rendered=false
on the h:selectOneMenu
component.
I have created 3 managed beans to test initialization sequence and managed beans are initialized in sequence they appear in xhtml, but before any of them JSF converter is created despite being last.
Form
<h:form>
<h:inputText value="#{creationTime1.string1}"/>
<h:inputText value="#{creationTime3.string3}"/>
<h:inputText value="#{creationTime2.string2}"/>
<h:selectOneMenu value="#{creationTime1.competitor}" rendered="false" converter="#{testConverter}" >
<f:selectItem itemValue="#{null}" itemLabel="#{msg.none}" />
<f:selectItems value="#{creationTime1.competitorList}" var="competitor" itemValue="#{competitor}"
itemLabel="#{competitor.idCompetitor}"/>
</h:selectOneMenu>
<h:commandButton value="GO" action="#{creationTime1.submit}"/>
</h:form>
Converter
@Named(value = "testConverter")
@ViewScoped
public class TestConverter implements Converter, Serializable {
@PostConstruct
private void init() {
System.out.println(System.currentTimeMillis() + " || TestConverter init");
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
...
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
...
}
}
Managed Beans (all the same with different names)
@Named(value = "creationTime2")
@ViewScoped
public class CreationTime2 implements Serializable {
private String string2;
public String getString2() {
System.out.println("getter srting2");
return string2;
}
public void setString2(String string2) {
this.string2 = string2;
}
@PostConstruct
private void init() {
System.out.println(System.currentTimeMillis() + " || CreationTime2 init");
}
}
Results on page visit
Info: START PHASE RESTORE_VIEW 1
Info: END PHASE RESTORE_VIEW 1
Info: START PHASE RENDER_RESPONSE 6
Info: 1451147920374 || TestConverter init
Info: 1451147920401 || CreationTime1 init
Info: getter string1
Info: 1451147920407 || CreationTime3 init
Info: getter string3
Info: 1451147920414 || CreationTime2 init
Info: getter string2
Info: END PHASE RENDER_RESPONSE 6
I also tried with @FacesConverter
instead of CDI bean and the results are the same.
- Why is converter invoked before any other bean?
- Why is converter created with
rendered=false
? Shouldn't it just passh:celectOneMenu
like it does not exist and don't create itself? - Can I somehow make converter create itself after these 3 beans (in order it appears on the xhtml page)?
Using
- Mojarra 2.2.7
- GlassFish 4.1