Hugues
Hugues

Reputation: 365

ManagedBean accessing another ManagedBean

I'm probably really close to the solution but I'm new with JSF and I don't see my mistake. I have a first SessionScoped Managed Bean that represents Business information (address, website, ...)

@Named(value = "businessController")
@SessionScoped
public class BusinessController implements Serializable {
    private Business current;
    @EJB private BusinessFacade ejbFacade;
    ....

I have a second SessionScoped Managed Bean that represents the logged in user

@Named(value = "loginController")
@SessionScoped
public class LoginController implements Serializable {

    private Login current;
    @EJB
    private LoginFacade ejbFacade;
    @ManagedProperty(value="#{businessController}")
    private BusinessController businessController;

    public BusinessController getBusinessController() {
        return businessController;
    }

    public void setBusinessController(BusinessController businessController) {
        this.businessController = businessController;
    }

When a user logs in, I set the current attribute from the loginController Depending on this current user, I want to set the business attribute from the businessController :

businessController.setCurrent(current.getBusiness());

My problem is that the businessController attribute is null !

I use NetBeans 7.0.1 and GlassFish 3.1 In debug mode, I can see a viewId variable with the value

>No current context (stack frame)<

Unfortunately it doesn't ring any bell to me.

Any help would be appreciated Thanks

Upvotes: 0

Views: 602

Answers (1)

Matt Handy
Matt Handy

Reputation: 30025

You are mixing JSF managed beans with CDI managed beans.

Your BusinessController is annotated with the CDI annotaion @Named but gets injected with the @ManagedProperty annotation (from JSF). CDI managed beans need to be injected with @Inject. No getter or setter needed in this case. If you tend to use CDI, make sure that you import the correct @SessionScoped:

CDI: javax.enterprise.context.SessionScoped

JSF: javax.faces.bean.SessionScoped

Try the following (After making sure to have the correct scope class imported):

 @Inject private BusinessController businessController;

Upvotes: 3

Related Questions