database 如何使用jstl从数据库中填充下拉列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3267837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:47:22  来源:igfitidea点击:

how to populate a drop down list from the database with jstl?

databasejspdrop-down-menujstl

提问by K_U

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:

我正在尝试使用 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;
    }
}

Here's my jsp:

这是我的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.

服务器日志和项目的构建输出中没有错误消息,但下拉列表为空。一直在努力解决这个问题,我不知道出了什么问题。我还尝试设置一个数据源并使用 jstl 迭代查询的结果集来完成它,但它的行为方式相同。

Now i'm not relying on any data source, but still the same results.

现在我不依赖任何数据源,但结果仍然相同。

Help appreciated, Thanks

帮助赞赏,谢谢

回答by Guido

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

第二点是这段代码

<option value="${aff}"></option>

doesn't show anything to the user, because it is rendered in HTML as an option with no text. Maybe it should be

不向用户显示任何内容,因为它在 HTML 中呈现为没有文本的选项。也许应该是

<option value="${aff}">${aff}</option>

回答by BalusC

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:

右键单击 webbrowser 中的 HTML 页面并查看源代码。您很可能会在其中看到未解析的 JSTL 标记。没错,您没有按照JSTL 核心标签库文档在 JSP 顶部导入 JSTL 标签库:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

(and further also don't forget to set the option label as mentioned by @Guido)

(另外也不要忘记设置@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.jarin /WEB-INF/libof your webapp and redeploy.

如果这不能解决问题并且您得到无法识别的 taglib 的编译错误,那么这意味着有问题的 servletcontainer 没有随 JSTL 一起提供,您必须自己安装 JSTL。它基本上很简单:只需将jstl-1.2.jar放入/WEB-INF/lib您的 web 应用程序中并重新部署。

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 @taglibin top of JSP.

如果您想知道为什么 JSP 标记不需要它,那是因为默认情况下它们已经被JspServlet. 任何其他 taglib 都应该像@taglib在 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 articleuseful to learn how to use JDBC code properly.

与实际问题无关:您的 JDBC 代码看起来不正确。你没有关闭连接,从而泄漏资源。如果您长时间重复运行此操作,那么数据库将耗尽连接并且您的应用程序将中断。您可能会发现本文对学习如何正确使用 JDBC 代码很有用。