C
C
Che_Bu_Rashka2016-11-21 16:42:15
Java
Che_Bu_Rashka, 2016-11-21 16:42:15

How can I call a bean method from another bean?

I have 2 beans navigation and user
How can I access the user bean method from the navigation bean ?

@Named("navigation")
@SessionScoped
public class NavigationController implements Serializable {

    private String outcome = null;

    @ManagedProperty(value = "#{user}")
    private UserBean userBean;   // <-- тут  держу ссылку на бин User

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }
.....
    public void handleMouseClick(ActionEvent e) {
            userBean.metod();   // <--- Тут  хочу вызвать метод у бина
    }
}

@Named("user")
@SessionScoped
public class UserBean implements Serializable {
    private Boolean success;

    public Boolean getSuccess() {
        return success;
    }

   public void metod() {
        success = false;
    }
}

NullPointer occurs when trying to call userBean.method(), how to connect them then?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aol-nnov, 2016-11-21
@Che_Bu_Rashka

where is DI?
the presence of a setter does not guarantee anything in this regard

C
Che_Bu_Rashka, 2016-11-21
@Che_Bu_Rashka

pancake. @Inject

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question