Question

I'm using JSF 2 and Sprig 3, and I want to migrate from using faces-config.xml to annotations.

old one : faces-config.xml :

<managed-bean> 
    <managed-bean-name>banqueBean</managed-bean-name>
    <managed-bean-class>commun.BanqueBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>banqueService</property-name>
        <value>#{banqueService}</value>
    </managed-property>
    <managed-property>
        <property-name>banqueId</property-name>
        <value>#{param.banqueId}</value>
    </managed-property>
</managed-bean>

new one :

public class BanqueBean{
    private Banque banque;
    @ManagedProperty(name = "banqueService", value = "#{banqueService}")
    private BanqueService banqueService;
    @ManagedProperty(value = "#{param.banqueId}")
    private String banqueId;

    // setters for banqueService and banqueId

the value of banqueId is set using :

<f:param value="#{banque.id}" name="banqueId" />

the problem is that when using faces-config.xml the "System" calls the setter of banqueService before the setter of parameter banqueId so that I can use banqueService inside setBanqueId method.

when using annotations it calls the setter of banqueId before banqueService so that I get null as a value of it.

why it inverses the call of this tow methods?

Was it helpful?

Solution

You should not rely on managed property setter method invocation order at all. This is nowhere definied in the specification.

Just hook at that point when JSF is finished with setting of all managed properties. That's the @PostConstruct annotated method.

@PostConstruct
public void init() {
    banque = banqueService.find(banqueId);
}

Stop doing business logic in setters, this is only necessary if you're still using legacy JSF 1.1 which didn't support @PostConstruct.


Unrelated to the concrete problem, are you aware of the new JSF2 <f:viewParam>? It might as well help you to get rid of this boilerplate in the bean and end up having only a Banque property and a reusable Converter.

See also:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top