0

I have an outputtext showing the number of services on the screen:

<h:outputText
        value="Services #{bean.counterManager.serviceCounter}">
</h:outputText>

and below it an accordionpanel that calls the getServices() method:

        <p:accordionPanel value="#{bean.services}" var="service">

In the getServices() method I increment the counter and when I debugged at the return point it was 143.

public List<Service> getServices() 
{
    if (this.services.isEmpty())
    {
        //Does other stuff, fills this.Services
        this.counterManager.incrementServiceCounter(someValue); //
    }
    return this.services;
}

But it appears 0 on screen, because getServices() is called after outputText calls getCounterManager() probably because the outputtext is above the accordionpanel on my XHTML.

I'd like for serviceCounter to show 143 and not 0, but I don't how to make it render after getLinhasStruct() is called, I can't put it the outputtext below accordion panel because that would mess with the layout of the page so how can I do that?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
StudioWorks
  • 483
  • 1
  • 6
  • 25

1 Answers1

2

Never do business logic in getters. You need to make sure that all your getters (and setters) are pure getters (and setters).

public List<Service> getServices() {
    return services;
}

After your IDE autogenerates them in the very bottom of the bean class, just ignore them forever. Do not touch them. Do as if they do not exist. You're supposed to perform business logic in action event listeners.

Your concrete problem is caused because those getter methods are during render response phase called in the same order as the components appear in the tree, and your code is incorrectly relying on something which is not controllable from inside the backing bean.

You did nowhere state the concrete functional requirement in the question, so it's a bit hard to point out the right approach, but there are in general the following approaches depending on when exactly you'd like to perform the business logic.

  1. During initial GET request? Use <f:viewAction>.
  2. Right before rendering the view? Use <f:event type="preRenderView">.
  3. During bean's initialization? Use @PostConstruct.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555