I am having this problem in this sample jsf project I created. Managed beans do not get instantiated. Bean class:
@ManagedBean(name=\"loginMB\")
@RequestScop
Managed beans are not created unless you specifically invoke one of their properties or methods. That happens for all the scopes, except the @ApplicationScoped
ones having @ManagedBean(eager=true)
which are specifically created when JSF context loads.
Here you're referencing #{loginMB}
from the view, but not #{userMB}
so there's no chance to have it created.
In order to instance your @SessionScoped
managed bean you can add following code in your login page:
<f:metadata>
<f:event type="preRenderView" listener="#{userMB.initialize}" />
</f:metadata>
Which sets a listener that is executed before page rendering. You can invoke here just an empty method (to execute it JSF will construct your bean if not already created).
Starting from JSF 2.2 you can replace the preRenderView
method for the new f:viewAction
, which has some benefits (it's parameterless and it doesn't get invoked on postbacks ):
<f:metadata>
<f:viewAction action="#{userMB.initialize}" />
</f:metadata>