@ManagedProperty does not reflect changes and keeps returning null

后端 未结 5 1972
花落未央
花落未央 2021-01-14 11:07

I\'m trying to inject the value of one sessionscoped bean into a viewscoped bean but it keeps returning null, here\'s a snippet:

import javax.faces.applicati         


        
5条回答
  •  时光说笑
    2021-01-14 11:42

    I just came across the same problem, and found out by chance, that it is not working, if I try with firefox (actually icedove under linux), but well working, if I try with the eclipse build-in browser.

    Even so this does not make sense to me, have you tried with different browsers already?


    michal777's answer is very well working. I have extended it to this:

        @ManagedProperty("#{nameBean}")
    private NameBean nameBean;  
    public NameBean getNameBean() { return nameBean; }
    public void setNameBean(NameBean nameBean) { this.nameBean = nameBean; }
    
    public NameBean getNameBean_Workaround() {
        FacesContext context = FacesContext.getCurrentInstance();
        return (NameBean) context.getApplication().evaluateExpressionGet(context,"#{nameBean}", NameBean.class);
    }
    

    and later on:

        if (nameBean != null) {
            nameBean.setName("achsooo");
        }
        else {
            getNameBean_Workaround().setName("achsooo2222");
        }
    

    Now, in the eclipse browser "achsooo" gets set, and in icedove "achsooo2222" gets set.

提交回复
热议问题