2

Perhaps this is a question I should be able to find documentation on, but I'm unfamiliar with a lot of the jargon so I'm struggling.

Basically, I'm using JSF2. I have a SessionScoped bean, and it uses a postconstruct init() method. I want the init() method to be called everytime the session starts, which works fine, but I also want it to be called every time the view loads.

Is there an easy way to do this?

Thanks!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1795370
  • 332
  • 2
  • 4
  • 18

1 Answers1

3

Replace @PostConstruct by <f:event type="preRenderView">.

<f:event type="preRenderView" listener="#{sessionScopedBean.init}" />

Better, however, is to split it into 2 beans: a @SessionScoped one and a @ViewScoped one. Then just reference the @ViewScoped one in the view instead and inject the @SessionScoped one as a property of the @ViewScoped one.

@Named
@ViewScoped
public class ViewScopedBean {

    @Inject
    private SessionScopedBean sessionScopedBean;

    @PostConstruct
    public void init() {
        // ...
    }

    // ...
}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Perfect thank you! And in case anyone is curious about this someone just found me this resource which is very useful ... http://www.theserverside.com/feature/Dependency-Injection-in-Java-EE-6-Part-5 – user1795370 Jan 08 '15 at 17:01