JSF 2 managed bean does not get instantiated

荒凉一梦 提交于 2019-12-01 12:32:32

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!