How to get managedbean property from another bean in JSF
Asked Answered
I

1

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?

Inflection answered 14/5, 2011 at 19:29 Comment(0)
B
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.

Backstop answered 14/5, 2011 at 19:30 Comment(5)
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.Inflection
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.Backstop
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?Inflection
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.Backstop
@Backstop 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?Brevet

© 2022 - 2024 — McMap. All rights reserved.