我尝试创建一个h:selectOneMenu
我遇到了一些错误,但最新的错误是在主题行中。我已经在这方面工作了一段时间,但我无法让它工作。
请帮忙
JPA:
package com.homework.jpa
import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
/**
* The persistent class for the GRIDMASTER database table.
*
*/
@Entity
@NamedQuery(name="Gridmaster.findAll", query="SELECT g FROM Gridmaster g")
public class Gridmaster implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private long gridid;
@Column(name="\"PROGRAM\"")
private String program;
public Gridmaster() {
}
public long getGridid() {
return this.gridid;
}
public void setGridid(long gridid) {
this.gridid = gridid;
}
public String getProgram() {
return this.program;
}
public void setProgram(String program) {
this.program = program;
}
}
EJB:
import com.homework.jpa.*;
import java.util.List;
@Stateless
public class GridMasterBean {
@PersistenceContext
private EntityManager em;
public List<Gridmaster> getDisplayGridList() {
return em.createQuery("FROM Gridmaster m", Gridmaster.class).getResultList();
}
支持bean:
@ManagedBean
public class gridMaster_backing {
private static final String String = null;
@EJB
private GridMasterBean ejb;
private List gridList = new ArrayList();
public String pickedGrid;
public String getPickedGrid() {
return pickedGrid;
}
public void setPickedgrid(String pickedGrid) {
this.pickedGrid = pickedGrid;
}
public void setGridList(List gridList) {
this.gridList = gridList;
}
public List getGridList() {
// produces List of Grids
gridList.clear();
List gridList = ejb.getDisplayGridList();
Iterator it = gridList.iterator();
while(it.hasNext()){
Gridmaster programCodes = (Gridmaster)it.next();
SelectItem sItem = new SelectItem();
//Long GridId = Gridmaster.getGridid();
//String progName = programCodes.getDescription() + " -- " + programCodes.getProgramCode();
sItem.setLabel(program);
sItem.setValue(gridid);
gridList.add(sItem);
}
return gridList;
}
}
XHTML:
<h:selectOneMenu value="#{gridMaster_backing.pickedGrid}">
<f:selectItems value="#{gridMaster_backing.gridList}" />
</h:selectOneMenu>
答案 0 :(得分:1)
问题是gridList.add(sItem)。您正在尝试在迭代时添加到列表中。而是使用索引迭代列表。然后在索引处添加。在索引处添加时,请务必相应地更新迭代器变量。