我在会话范围内有两个托管bean。第一个是简单的元素列表。
@ManagedBean (name = TrainList.BEAN_NAME)
@SessionScoped
public class TrainList extends ListBasic<TrainObj> {
private static final long serialVersionUID = 1L;
public static final String BEAN_NAME = "trainList";
public TrainList () {
super();
}
}
其次表示列表中特定记录的详细信息,并在用户单击第一个列表中的记录后调用。
@ManagedBean (name = TrainForm.BEAN_NAME)
@SessionScoped
public class TrainForm extends TrainObj {
private static final Logger logger = Logger.getLogger(TrainForm.class.getName());
public static final String BEAN_NAME = "trainForm";
@ManagedProperty(value = "#{trainList}")
private TrainList trainList; /* + getter and setter */
public TrainForm () {
super();
}
public void refreshList(){
if (trainList != null) {
logger.debug(" trainlist is not null");
trainList.refreshList();
}
else {
logger.debug(" trainlist is NULL");
}
}
所以list bean在第二个之前初始化。 但是从第二个访问第一个bean时仍然是null?
[2014-11-12 13:18] DEBUG [http-apr-8080-exec-7] - com.kropla.TrainForm.update(TrainForm.java:xx) - trainlist is NULL
有什么问题?
[请不要投票 - 我已经搜索了答案,但没有找到合适/合适的答案]
我可以列出所有托管bean(https://stackoverflow.com/a/15503044/2303190),这个bean在列表中。但为什么我会变为空?
答案 0 :(得分:0)
尝试像这样获取bean。我在我的应用程序中使用了以下代码,但是使用@ViewScoped你的是SessionScoped,尝试这可能有用。
FacesContext context = FacesContext.getCurrentInstance();
TrainList trainList = context.getApplication().evaluateExpressionGet(context, "#{trainList}",TrainList.class);
答案 1 :(得分:0)
似乎问题与第一个初始化(调用)第二个bean的方式有关。
private String returnDetailPageName(TrainObj entityObj) {
Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
TrainForm formBean = new TrainForm();
formBean.setMe(entityObj); //setter for the entity vproperties
session.put(formBeanName , formBean);
return "goProdTraining"; //go to detail page
}
此&#34;会话&#34;调用bean的方法是创建一个没有存储在JSF中的新bean。 通过ManagedProperty更改调用bean属性,使用PostConstruct将父bean分配给子bean(以防止循环循环):
@ManagedBean (name = TrainList.BEAN_NAME)
@SessionScoped
public class TrainList extends ListBasic<TrainObj> {
private static final long serialVersionUID = 1L;
public static final String BEAN_NAME = "trainList";
public TrainList () { super();}
@ManagedProperty (value = "#{trainForm}")
private TrainForm trainForm; //with getter&setter
@PostConstruct
private void setChildBeanProperty(){
this.trainForm.setTrainList(this);
}
}
@ManagedBean (name = TrainForm.BEAN_NAME)
@SessionScoped
public class TrainForm extends TrainObj {
private static final Logger logger = Logger.getLogger(TrainForm.class.getName());
public static final String BEAN_NAME = "trainForm";
private TrainList trainList; /* + getter and setter */
public TrainForm () {
super();
}
public void refreshList(){
if (trainList != null) {
logger.debug(" trainlist is not null");
trainList.refreshList();
}
else {
logger.debug(" trainlist is NULL");
}
}