[英]@ManagedProperty does not reflect changes and keeps returning null
我試圖將一個會話范圍的 bean 的值注入一個視圖范圍的 bean,但它一直返回 null,這是一個片段:
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 時,我不確定為什么。
最近我也遇到了通過@ManagedProperties
注入嵌套托管bean 屬性的問題。 一旦注射它就永遠不會改變。 我通過在 getter 中評估 EL 而不是注入它來解決了這個問題。
試試看:
public String getUserName() {
FacesContext context = FacesContext.getCurrentInstance();
return (String) context.getApplication().evaluateExpressionGet(context,"#{user.userName}", String.class);
}
您還可以嘗試注入整個user
bean 並在 getter 中從中獲取userName
字段。
在所有 setter/getter 到位的情況下,由於用戶 class 中缺少空構造函數,我遇到了同樣的問題( null對用戶的引用)。
在您提供的示例中, dataBrowser
和用戶 bean 在構建表之前被實例化,因此引用#{dataBrowser.userName}
應該已經找到正確注入的userName @ManagedProperty
(不是@PostConstruct
問題)。
我剛剛遇到了同樣的問題,偶然發現,如果我嘗試使用 firefox(實際上是 icedove 在 linux 下),它不起作用,但是如果我嘗試使用 eclipse 內置瀏覽器,它工作得很好。
即便如此這對我來說沒有意義,你已經嘗試過不同的瀏覽器了嗎?
michal777 的回答非常有效。 我把它擴展到這個:
@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);
}
后來:
if (nameBean != null) {
nameBean.setName("achsooo");
}
else {
getNameBean_Workaround().setName("achsooo2222");
}
現在,在 eclipse 瀏覽器中設置了“achsooo”,在 icedove 中設置了“achsooo2222”。
#{user.userName}
被 JSF 解釋為getUser().getUserName()
因此,最好User
類型的@ManagedProperty
及其 getter/setter 方法getUser
/ setUser
。 這樣您就可以通過#{user.userName}
訪問用戶名。
我有這個問題,這個問題實際上是雙重的。 (另請注意,@ManagedProperty 只會在 @ManagedBean class 中工作,並且如果 @ManagedProperty class 與 scope 相同或更小(應用程序、session、視圖、請求等)。)以下是我修復它的方法:
問題 1: JSF 很愚蠢,沒有正確處理abstract
類中的@ManagedProperty
注入。
解決方案:
@ManagedBean
的 class 都用@ManagedProperty
注釋。abstract
class 不使用@ManagedProperty
注釋,而是僅提供非抽象類將分別覆蓋的抽象 getter 和 setter 方法。abstract
類中使用abstract
類的 getter 方法而不是 @ManagedProperty 本身。 問題 2: JSF 是愚蠢的,不能在不是由 JSF 創建的 @ManagedBean 類中正確處理@ManagedProperty
注入(即您正在使用new
自己創建這些類)。
解決方案選項:
@ManagedProperty
的 class。MyClass example = Utils.getELValue("EL Expression Goes Here", MyClass.class);
public static <T> T getELValue(final String elName, final Class<T> clazz) {
FacesContext fc = FacesContext.getCurrentInstance();
return (T) fc.getApplication().getELResolver().getValue(fc.getELContext(), null, elName);
// Potential (untested) alternative:
// ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession().getAttribute("")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.