0

I'm a beginner in JSF and I'm having problems accessing data stored in one session scoped bean from another bean. I've read similar questions here, but they didn't help.

Anyway, here's one bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "loginBean")
@SessionScoped
public class loginBean
{

    private String username;
    private String password;
/*etc*/

I want to access the username and password from that bean in this second bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
/*rest of the imports*/
@ManagedBean
@SessionScoped
public class glavnaStrBean 
{
@ManagedProperty(value="#{loginBean}")
    loginBean logBin;


    public loginBean getLogBin() {
        return logBin;
    }

    public void setLogBin(loginBean logBin) {
        this.logBin = logBin;
    }

Problem is that variable logBin is always null.

Any ideas what I'm doing wrong here?

AndrejaKo
  • 1,721
  • 5
  • 25
  • 41
  • Are you sure are using javax.faces.bean.* packages? – Johny T Koshy Sep 14 '13 at 05:19
  • @johny That's a good question. I have ManagedProperty, ManagedBean and SessionScoped imported. Eclipse-EE isn't complaining about any missing dependencies. Should I add any other packages? – AndrejaKo Sep 14 '13 at 05:38
  • Just to make sure, how are you accessing the logBin? Remember you will not be able to used injected fields in the constructor – Aksel Willgert Sep 14 '13 at 07:12
  • If you are using javax.faces.bean.ManagedBean and javax.faces.bean.SessionScoped then you can use javax.faces.bean.ManagedProperty to inject beans. If you are using cdi then you should use @Inject instead of ManagedProperty. Dont forget to add beans.xml if you are using CDI. – Johny T Koshy Sep 14 '13 at 07:22
  • Andre: this wasn't exactly what Johny asked. Johny asked if the `import` lines of your `@ManagedBean` and `@SessionScoped` annotations are referring the `javax.faces.bean` package. E.g. `import javax.faces.bean.ManagedBean;` and so on. Look in top of your class. Those annotation names exist namely also in a different package. If you have imported them from the wrong package, then that would explain why they actually didn't work. – BalusC Sep 14 '13 at 19:46
  • @Aksel Willgert I'm accessing it in a standard method. I have a command button which calls a method in the glavnaStrBean and that method needs data stored in the loginBean. – AndrejaKo Sep 14 '13 at 19:56
  • @johny I'm using imports from the javax.faces.bean. package. – AndrejaKo Sep 14 '13 at 20:06
  • @BalusC I've added exact import statements to the question. – AndrejaKo Sep 14 '13 at 20:06

0 Answers0