我有这个作为 JSP 页面,我想要一个组合框来填充数据库中表中的值。我有一个类Assembly和一个类Assembly,在Assemblys中,我将每条记录都作为Assembly的实例并将其放入列表中。但我无法在 JSTL 中检索它?为什么是这样?
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
.....
<select name = "assembly">
<c:forEach var ="item" items="${Assemblys}">
<option value="${item.id}">${item.Name}</option>
</c:forEach>
</select>
这作为类组装类
public class Assembly {
public String Name;
public int cost;
public int id;
public Assembly(String Name, int cost, int id) {
this.Name = Name;
this.cost = cost;
this.id = id;
}
}
这是类Assemblys
public class Assemblys {
List<Assembly> list = new ArrayList<Assembly>();
public Assemblys(){
list = getAssemblys();
}
private List<Assembly> getAssemblys() {
try {
Connection conn = MysqlConnect.conn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String Query = "Select * from tbl_assembly";
pstmt = conn.prepareStatement(Query);
rs = pstmt.executeQuery();
while (rs.next()) {
Assembly ass = new Assembly(rs.getString(2),rs.getInt(3),rs.getInt(1));
list.add(ass);
}
conn.close();
} catch (SQLException ex) {
Logger.getLogger(Assemblys.class.getName()).log(Level.SEVERE, null, ex);
}
return list;
}
}
为什么我的下拉列表是空白的?我不明白。我认为这是正确的代码?
提前谢谢了!