17

I searched similar questions but I'm a bit confused. I have a login page, so LoginBean also which is;

@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {    
    private String password="";
    private String image="";
    @ManagedProperty(value = "#{loginBeanIdentityNr}")
    private String identityNr="";
...

after success, navigates to orderlist page, so I have also OrderBean.

@ManagedBean(name = "OrderBean")
@SessionScoped
       public class OrderBean {
            List<Ordery> sdList;

            public List<Order> getSdList() {

                try {

                    String identityNr ="";
                    ELContext elContext = FacesContext.getCurrentInstance().getELContext();
                    LoginBean lBean = (LoginBean) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "loginBean");
                    identityNr =lBean.getIdentityNr();
                    sdList = DatabaseUtil.getOrderByIdentityNr(identityNr);
    ...
    }

I don't need the whole LoginBean, just ManagedProperty "loginBeanIdentityNr". But this code below doesn't work (of course);

identityNr = (String) FacesContext.getCurrentInstance()
                        .getApplication().getELResolver()
                        .getValue(elContext, null, "loginBeanIdentityNr");

this time it returns null to me. I think if I need whole bean property, I can inject these beans, right? So, do you have any suggestions for this approach? can<f:attribute> be used?

asyard
  • 1,743
  • 5
  • 21
  • 43

1 Answers1

45

The @ManagedProperty declares the location where JSF should set the property, not where JSF should "export" the property. You need to just inject the LoginBean as property of OrderBean.

public class OrderBean {

    @ManagedProperty(value="#{loginBean}")
    private LoginBean loginBean; // +setter

    // ...
}

This way you can access it in the OrderBean by just

loginBean.getIdentityNr();

Alternatively, if you make your OrderBean request or view scoped, then you can also set only the identityNr property.

public class OrderBean {

    @ManagedProperty(value="#{loginBean.identityNr}")
    private String identityNr; // +setter

    // ...
}

Unrelated to the concrete problem: initializing String properties with an empty string is a poor practice.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • hi BalusC, but this time in OrderBean, I will have unnecessary properties of loginbean. isn't it waste of heap for program? i just need identity nr. not captcha value or name. – asyard May 14 '11 at 19:35
  • 1
    Uh, it's just a reference. It does not make a copy of the whole bean in memory or something. It points to **exactly the same** bean as you already have in session. Java is object oriented, not procedural or something. Even more, double-referencing a String instead of a javabean is potentially more expensive. – BalusC May 14 '11 at 19:37
  • thank for answer. i will vote all your replies as soon as i get enough reputation:) But, just curious, is there another way, like facescontext.getblabla().. or in jsf page as parameter without injecting? – asyard May 14 '11 at 19:41
  • 1
    Using `FacesContext#getBlabla()` should be avoided as much. You could pass it as request parameter, yes, but why would you transfer the control to the client side? This way the client would be able to edit it and all your code would break. – BalusC May 14 '11 at 19:44
  • @BalusC How does JSF distinguish between two instances of the same bean when performing the injection? Let's say we have 2 instances of LoginBean with view scope, how would JSF know which one I want? – futureelite7 Jul 07 '14 at 03:24