如何从JSF中的另一个bean获取ManagedBean属性 [英] How to get managedbean property from another bean in JSF

查看:147
本文介绍了如何从JSF中的另一个bean获取ManagedBean属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了类似的问题,但有点困惑.我有一个登录页面,所以LoginBean也是如此;

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="";
...

成功后,导航到订单列表页面,所以我也有OrderBean.

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);
    ...
    }

我不需要整个LoginBean,只需ManagedProperty"loginBeanIdentityNr".但是下面的这段代码不起作用(当然);

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");

这一次它向我返回null. 我认为如果需要整个bean属性,可以注入这些bean,对吗?那么,您对此方法有什么建议吗?可以使用<f:attribute>吗?

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?

推荐答案

@ManagedProperty声明JSF应该在其中设置属性的位置,而不是JSF应该导出"该属性的位置.您只需将LoginBean作为OrderBean的属性注入.

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

    // ...
}

这样,您只需在OrderBean中即可访问它

This way you can access it in the OrderBean by just

loginBean.getIdentityNr();


或者,如果您发出OrderBean请求或查看范围,则也只能设置identityNr属性.


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

    // ...
}


不相关与具体问题:用空字符串初始化String属性是一种糟糕的做法.


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

这篇关于如何从JSF中的另一个bean获取ManagedBean属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆