如何使用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>
服务器日志和项目的构建输出中没有错误消息,但 drop下拉列表为空。一直在努力解决这个问题,我不知道出了什么问题。我还尝试设置一个数据源并使用 jstl 迭代查询的结果集来完成此操作,但效果相同。
现在我不依赖任何数据源,但结果仍然相同。
帮助表示赞赏, 谢谢
I know this has been asked before, but it just doesn't work for me.
I'm trying to populate a drop down list from a database using jstl, here's the class i use as a 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;
}
}
Here's my 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>
There are no error messages in the server log and in the build output of the project, but the drop down list is empty. Been struggling with this, and I have no clue as to what's wrong. I also tried to set a data source and do it using jstl iterating through the resultset of the query, but that acted the same way.
Now i'm not relying on any data source, but still the same results.
Help appreciated,
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尚未测试您的代码,但请在服务器端添加断点以检查列表中有多少元素。也许服务器端也是空的。
第二点是此代码
不会向用户显示任何内容,因为它在 HTML 中呈现为没有文本的选项。也许应该是
I have not tested your code, but please add a breakpoint in the server side to check how many elements you have in the list. Maybe it is also empty in the server side.
The second point is that this code
doesn't show anything to the user, because it is rendered in HTML as an option with no text. Maybe it should be
右键单击网络浏览器中的 HTML 页面并查看源代码。您很有可能会在其中看到未解析的 JSTL 标记。是的,您没有按照 JSTL 核心 taglib 文档:(
此外也不要忘记设置 @Guido 提到的选项标签)
如果这不能解决问题,您会得到无法识别的 taglib 的编译错误,则意味着有问题的 servletcontainer 未随 JSTL 一起提供,您必须自己安装 JSTL。这基本上很简单:只需删除 jstl-1.2.jar 在您的 web 应用程序的
/WEB-INF/lib
中并重新部署。如果您想知道为什么 JSP 标记不需要这样做,那是因为它们默认已被
JspServlet
识别。任何其他标记库都应在 JSP 顶部显式声明为@taglib
。与实际问题无关:您的 JDBC 代码看起来不正确。您没有关闭连接并因此泄漏资源。如果您长时间重复运行此命令,那么数据库将耗尽连接,您的应用程序将崩溃。您可能会发现这篇文章对于了解如何正确使用 JDBC 代码。
Rightclick the HTML page in webbrowser and View Source. Big chance that you see the JSTL tags unparsed in there. That is right, you didn't import the JSTL taglib in top of JSP as per the JSTL core taglib documentation:
(and further also don't forget to set the option label as mentioned by @Guido)
If that doesn't fix the problem and you get compilation errors of unrecognized taglib, then it means that the servletcontainer in question doesn't ship with JSTL and that you'll have to install JSTL yourself. It's basically simple: just drop jstl-1.2.jar in
/WEB-INF/lib
of your webapp and redeploy.If you ever wonder why that's not needed for JSP tags, well, that's because they are by default already recognized by the
JspServlet
. Any other taglibs should be declared explicitly as@taglib
in top of JSP.Unrelated to the actual problem: your JDBC code doesn't look right. You're not closing connections and thus leaking resources. If you run this repeatedly over a long period, then the DB will run out of connections and your application will break. You may find this article useful to learn how to use JDBC code properly.