@ManagedProperty不反映更改,并始终返回null [英] @ManagedProperty does not reflect changes and keeps returning null
问题描述
我试图将一个sessionscoped bean的值注入到viewscoped bean中,但是它一直返回null,这是一个代码段:
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.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
//Class for managing the current logged-in user
@ManagedBean(name="user")
@SessionScoped
public class User implements Serializable{
private String userName;
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return this.userName;
}
它用在:
@ManagedBean(name="databrowser")
@ViewScoped
public class dataBrowser implements Serializable {
private List<UploadData> dataItems;
private SelectItem[] dataTypeOptions, qualityOptions, accessOptions;
private UploadData selectedData;
private String filelocation;
@ManagedProperty(value="#{user.userName}")
private String userName;
public String getUserName() {
return this.userName;
}
dataBrowser用于填充Primefaces数据表,当它被称为userName为null且我不确定为什么时.
dataBrowser is used to populate a Primefaces datatable, when it's called userName is null and I'm not sure why.
推荐答案
最近,我也对通过@ManagedProperties
注入嵌套的托管Bean属性存在问题.一旦注入,它就永远不会改变.我通过在吸气剂中评估EL而不是注入EL来进行变通.
Recently I have problem with injecting nested managed bean properties by @ManagedProperties
too. Once injected it never changed. I did a workaround by evaluating EL in getter instead of injecting it.
尝试:
public String getUserName() {
FacesContext context = FacesContext.getCurrentInstance();
return (String) context.getApplication().evaluateExpressionGet(context,"#{user.userName}", String.class);
}
您还可以尝试注入整个user
bean,并在getter中从中获取userName
字段.
You can also try injecting entire user
bean and get userName
field from it in getter.
这篇关于@ManagedProperty不反映更改,并始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!