我知道這已被問過,但它不適合我。如何使用jstl從數據庫填充下拉列表?
我想使用JSTL來填充下拉從數據庫列表,這是我作爲一個useBean的使用類:
public class feedData {
Connection con;
public feedData() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "******";
con = DriverManager.getConnection(url, "**", "**");
}
public ArrayList<String> getUnis() throws SQLException {
ArrayList<String> uniList = new ArrayList<String>();
String tryquery = "select aff from libra.smalluniqdbtmp";
Statement stmt2 = con.createStatement();
ResultSet rs1 = stmt2.executeQuery(tryquery);
while (rs1.next()) {
uniList.add(rs1.getString("aff"));
}
return uniList;
}
public ArrayList<String> getRI() throws SQLException {
ArrayList<String> RIList = new ArrayList<String>();
String tryquery = "select interest from libra.riuniqdb";
Statement stmt2 = con.createStatement();
ResultSet rs1 = stmt2.executeQuery(tryquery);
while (rs1.next()) {
RIList.add(rs1.getString("aff"));
}
return RIList;
}
}
,這裏是我的jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="obj" scope="page" class="uiLibraWeb2Pkg.feedData" />
<h1>Hello World!</h1>
<table border="1">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td> <form action="response.jsp">
<strong>Select a university:</strong>
<select name="affiliation">
<c:forEach var="aff" items="${obj.unis}">
<option value="${aff}"></option>
</c:forEach>
</select>
</form>
</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>
有沒有錯誤消息在服務器日誌和項目的生成輸出中,但下拉列表爲空。一直在努力,我不知道什麼是錯的。我也嘗試設置一個數據源,並使用jstl遍歷查詢的結果集來執行此操作,但是其行爲方式相同。
現在我不依賴任何數據源,但仍然是相同的結果。
幫助表示讚賞,感謝
感謝您的建議,工作。我愚蠢。 – 2010-07-16 20:27:31