【发布时间】:2012-04-18 22:53:33
【问题描述】:
我正在尝试将一个 sessionscoped bean 的值注入到 viewscoped bean 中,但它一直返回 null,这是一个 sn-p:
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 时为空,我不知道为什么。
【问题讨论】:
-
你有
userName的二传手吗?你需要一个才能使注入工作。 -
...您还需要在 @ViewScoped bean 中设置一个设置器。
-
这会引发异常而不是返回
null。 -
抱歉,复活节周末回复晚了 - 是的,很抱歉,我在 getter 代码之后直接有这个:public void setUserName(String userName) {this.userName = userName;}
-
解决了它 -> 最终移动了我的函数并使用@PostConstruct 来确保在使用变量之前进行注入
标签: jsf null managed-property