JSF 2 managed bean does not get instantiated

后端 未结 1 729
清酒与你
清酒与你 2021-01-15 10:45

I am having this problem in this sample jsf project I created. Managed beans do not get instantiated. Bean class:

@ManagedBean(name=\"loginMB\")
@RequestScop         


        
相关标签:
1条回答
  • 2021-01-15 11:18

    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>
    
    0 讨论(0)
提交回复
热议问题