Reputation: 1743
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?
Upvotes: 17
Views: 40978
Reputation: 1108537
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.
Upvotes: 45