JSF call a bean method before the page load

jsf

Solution

You can use the `@PostConstruct` annotation to automatically invoke your init method:

@PostConstruct
public void init() {
  // do something
}

This method is automatically invoked after construction of the bean.

Your solution looks more like a `f:event` with `type="preRenderView"` but this can't be used because the `c:if` tags are evaluated during view build time, while the `f:event` (respectively your solution) runs right before the view is rendered during render response phase. Have a look at this question and this question to get details.

Update: As you commented you are using a `@SessionScoped` bean where `@PostConstruct` is only called once per session and not on every page load. In this case another solution would be to call your `init` method as first statement in your `conditionsCheck` method (nearly the same as your suggestion with fake `c:if` boolean init). You could also use a custom `PhaseListener` but I guess that would be somewhat overdosed for this problem.

See also:

- Why use @PostConstruct?

Problem

I have to call a init method on my bean as first action on the page load, I have tried to simply call `#{bean.init}` at the very beginning of my page, but I have seen that the `<c:if>` tests are performed before the `init()`. I have something like ``` #{bean.init} <c:if test="#{bean.conditionsCheck}">...</c:if> ``` and the `conditionsCheck()` method is called before the `init()`, how can I fix it and call `init()` as really first thing?

Original source

Related problems