-1

我有这个作为 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;
    }
}

为什么我的下拉列表是空白的?我不明白。我认为这是正确的代码?

提前谢谢了!

4

3 回答 3

1

首先,您应该将 getter 添加到 Assembly 类。

public class Assembly {
 public String Name;
 public int cost;
 public int id;

 public String getName() {
  return this.Name;
 } 

 public int getId() {
   return this.id;
 }

 public Assembly(String Name, int cost, int id) {
    this.Name = Name;
    this.cost = cost;
    this.id = id;
 }
}

其次将getList方法添加到Assemblys

public class Assemblys {
  List<Assembly> list = new ArrayList<Assembly>();

  public List<Assembly> getList() {
        return this.list;
  } 

  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;
    }
}

现在jsp

 <select name = "assembly">
 <c:forEach var ="item" items="${Assemblys.list}">
 <option value="${item.id}">${item.Name}</option>
 </c:forEach>
 </select>
于 2013-01-24T07:41:34.150 回答
1

您需要一个 servlet 或在 jsp 中使用 scriptlet(标记为不好的做法)来调用类程序集。您指定的标签,我猜您正在使用 servlet/jsp 应用程序。

jsp中使用的名称Assemblys应该在请求对象中作为属性。您为程序集指定的键可用于获取列表。

于 2013-01-24T07:56:13.507 回答
0

代替

<c:forEach var ="item" items="${Assemblys}">

<c:forEach var ="item" items="${Assemblys.list}">

编辑 :

结合@user2006574 和@AntonIlinchik 的答案,您必须将getter 添加到Assembly,将Assemblys 中的getter 更改为公共访问。

另外,发布Assemblys调用构造函数的代码。

于 2013-01-24T07:40:21.660 回答