Access session scoped bean from request scoped bean

前端 未结 3 585
星月不相逢
星月不相逢 2020-12-10 00:08

I\'m trying to use a pattern found on a IceFaces page.(I\'m not using IceFaces, using PrimeFaces)

In this case I have two beans:

UserController and

3条回答
  •  萌比男神i
    2020-12-10 00:33

    Instead of creating a new UserModel instance in your UserController, inject it with @ManagedProperty.

    In UserController:

    @ManagedProperty(value="#{userModel}")
    private UserModel model;
    
    // add getter and setter for userModel (important!)
    

    Then you don't have to instantiate it in the constructor and will always get the session scoped instance of UserModel in your controller.

    UPDATE:

    I think you are complicating the login process with your strict MVC approach. In JSF the borderlines between model, view and controller are somewhat blurred or overlapping.

    I recommend reading this interesting question and answers and especially this answer for more information on that topic.

    As to your concrete problem. I am not quite sure what is the reason but what you should definitely avoid is to instantiate managed bean by youself and fiddle around with both injected and self-initialized instances of beans.

    Also I would recommend to merge your beans together into a single bean. Then you don't have the problems with circular dependencies and null references.

提交回复
热议问题