在JSP页面中填充下拉列表

发布于 2024-10-09 02:37:28 字数 628 浏览 2 评论 0原文

我在 netbeans 中工作,我有一个 JSP 页面,其中有一个下拉列表,需要从 MySQL 数据库中提取选项。我不知道如何让 MySQL 数据库填充下拉列表。

<form action="student/studentQueryResponse.jsp">
    <strong>Select a student:</strong>
    <select name="studentID">
        <c:forEach var="row" items="${student.rowsByIndex}">
            <c:forEach var="column" items="${row}">
                <option <c:out value="${column}"/>
                </option>
            </c:forEach>
        </c:forEach>
    </select>
    <input type="submit" value="submit" name="submit" />
</form>

I am working in netbeans, I have a JSP page that has a dropdownlist that needs to pull the options from a MySQL database. I cannot figure out how to get the MySQL database to populate the dropdownlist.

<form action="student/studentQueryResponse.jsp">
    <strong>Select a student:</strong>
    <select name="studentID">
        <c:forEach var="row" items="${student.rowsByIndex}">
            <c:forEach var="column" items="${row}">
                <option <c:out value="${column}"/>
                </option>
            </c:forEach>
        </c:forEach>
    </select>
    <input type="submit" value="submit" name="submit" />
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2024-10-16 02:37:28

你从哪里粘贴代码?它使用 rowsByIndex 属性,因此它显然是与 jstl 标记一起使用(并且它是由了解它的人编写的)。另一方面,在这种情况下使用两个嵌套的 forEach 子句是没有意义的:您最终将为每个学生数据(姓名、姓氏等)提供一个单独的选项,而不是为单个学生数据提供一个选项。学生。看起来原始代码似乎是针对某种数据网格的,并且在没有任何理解的情况下进行了修改。

恕我直言,我同意你还没有准备好编写应用程序的观点 - 并且你不会通过尝试学到很多东西,因为你很快就会遇到越来越难的主题。如果你被迫继续应用,请尝试放弃 JSF 并专注于 JSP/JSTL,我相信它对初学者来说有更少的警告,并且会让你更容易学习 Web 应用程序的基础知识。

也就是说,您正在寻找的答案是:

<sql:setDataSource dataSource="jdbc/db" />
<sql:query var="students">
    select * from students
</sql:query>
<form action="student/studentQueryResponse.jsp">
    <strong>Select a student:</strong>
    <select name="studentID">
        <c:forEach var="row" items="${students.rowsByIndex}">
            <option id="<c:out value="${row[0]}"/>"><c:out value="${row[1]}"/></option>            
        </c:forEach>
    </select>
    <input type="submit" value="submit" name="submit" />
</form>

您必须用自己的 sql 查询替换 sql 查询,并且必须在应用程序服务器和 jdbc/db 中注册数据源 jdbc/db >web.xml 文件。当然,您还需要一个 MySQL 驱动程序。我假设您需要学生的 ID,并且它是查询的第一列。

Where did you paste the code from? It uses the rowsByIndex property, so it is clearly meant for use with jstl tag (and it is written by someone who knew it). On the other hand, using two nested forEach clauses makes no sense in this context: you are going to end up with a separate option for each of the students data (name, last name, whatever), instead of a single option for a single student. It looks as if the original code was for a data grid of some kind, and has been modified without a trace of understanding.

With all due respect, I share the view that you are not ready to write the application - and you are not going to learn much by trying, as you will soon come to harder and harder topics. If you are forced to continue the application, try dumping JSF and concentrate on JSP/JSTL, I believe it has less caveats for a beginner and it will make it easier for you to learn the basics of web applications.

That said, the answer you are looking for is:

<sql:setDataSource dataSource="jdbc/db" />
<sql:query var="students">
    select * from students
</sql:query>
<form action="student/studentQueryResponse.jsp">
    <strong>Select a student:</strong>
    <select name="studentID">
        <c:forEach var="row" items="${students.rowsByIndex}">
            <option id="<c:out value="${row[0]}"/>"><c:out value="${row[1]}"/></option>            
        </c:forEach>
    </select>
    <input type="submit" value="submit" name="submit" />
</form>

You will have to substitute the sql query with your own, and you will have to register a datasource jdbc/db in your application server and in the web.xml file. Of course, you will also need a MySQL driver. I assumed that you willl need student's id and that it is the first column of your query.

笑忘罢 2024-10-16 02:37:28

是的,BalusC 的担忧是正确的,如果您不使用 JSF,那么它就不是一个 JSF 项目。如果您将其设为 JSF 项目,那么 selectOneItem selectOneMenu 可以为您解决问题。就数据库连接而言,干净的方式是在支持 bean 中建立连接的理想选择,可以使用数据源,也可以使用普通的 jdbc 或其他方法。

Yes BalusC's concerns are right, if you are not using JSF then its not a JSF Project. If you make it a JSF project then selectOneItem selectOneMenu can do the trick for you. As far as database connectivity is concerned then clean way would be ideally to make connection in you backing bean, either using datasources or just plain jdbc or other methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文