【发布时间】:2013-05-10 06:03:35
【问题描述】:
当我在控制台打印所选项目时,它总是返回为 null
这是在我的 ManagedBean 中创建 SelectItem 的方法:
public List<String> getlisteMatricule() throws HibernateException
{
List<String> matricules = new ArrayList<String>();
for (Vehicule v : vehiculedao.getAll())
{
matricules.add(v.getMatricule());
System.out.println(v.getMatricule());
}
return matricules ;
}
public List<SelectItem> getAllMatricules()
{
List<SelectItem> options = new ArrayList<SelectItem>();
List<String> listMatricules = getlisteMatricule();
for (String mat : listMatricules)
{
options.add(new SelectItem(mat));
System.out.println("items = " + new SelectItem(mat));
}
return options ;
}
这是我模型中的变量,其中包含 getter 和 setter 以及构造函数:
public class Program
{
private int id_progf;
private int nbrHeure;
private float montantGlobal;
private String commentaire;
private int cin_mon;
private String matricule;
private int cin_cand;
///gettersand setters
.... }
从数据库(列表)中获取变量的方法
@Override
public Vehicule getMatricule(String matricule) {
Session session = HibernateUtil.currentSession();
Vehicule v=(Vehicule)session.get(Vehicule.class, matricule);
return v;
}
最后是我的 xhtml 文件,它包含以下形式:
<h:panelGrid columns="2" >
<h:outputText value="Moniteur :" />
<h:selectOneMenu id="listeNomPrenom" title="Nom et Prenom" value="{#programMB.np}">
<f:selectItems value="#{moniteurMB.allNomPrenom}" />
</h:selectOneMenu>
<h:outputText value="Vehicule :" />
<h:selectOneMenu id="ListeMatricules" title="Matricules" value="{#programMB.program.matricule}">
<f:selectItems value="#{vehiculeMB.allMatricules}" />
</h:selectOneMenu>
<h:outputText value="Nombre heures:" />
<p:inputText value="#{programMB.program.nbrHeure}" />
</h:panelGrid>
<p:commandButton value="Save" action="#{programMB.ajouterProg}" />
【问题讨论】:
-
这似乎是错误的:value="{#programMB.np}"。你应该在大括号之前有#
-
如果
vehiculedao.getAll()返回一个空列表,为什么您认为问题出在JSF 方面?问题实际上出在getAll()方法背后的代码中不是更合乎逻辑吗?所有这些 JSF 代码都是纯粹的噪音,您应该以在 Hibernate 上下文中询问的方式重写问题。 -
在一个不相关的说明中:请停止在 getter 方法中执行业务逻辑。