Java 使用数据库信息填充 JSP 下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2901222/
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-08-13 14:18:31  来源:igfitidea点击:

Populate JSP dropdown with database info

javajspjdbcjstl

提问by Cano63

I'm trying to populate a JSP dropdown from a database table.

我正在尝试从数据库表中填充 JSP 下拉列表。

Here's the code that will create the array and fill it with the database info:

这是将创建数组并用数据库信息填充它的代码:

// this will create my array 
public static ArrayList<DropDownBrands> getBrandsMakes() {
    ArrayList<DropDownBrands> arrayBrandsMake = new ArrayList<DropDownBrands>();
    while (rs.next()) {     
        arrayBrandsMake.add(loadOB(rs));
    }
    return arrayBrandsMake;
}

// this will load my array object
private static DropDownBrands loadOB(ResultSet rs) throws SQLException {
    DropDownBrands  OB = new DropDownBrands();
    OB.setBrands("BRAN");
    return OB;
}

How do I call that class from my JSP and populate the dropdown?

如何从我的 JSP 调用该类并填充下拉列表?

采纳答案by Casey

I would suggest trying to stay away from mixing the display and model code. Keep all of your html in the jsp page and create model backing objects that provide the information you need. For example, let's say you have a simple Java class that has a list of objects:

我建议尽量避免混合显示和模型代码。将所有 html 保留在 jsp 页面中,并创建提供所需信息的模型支持对象。例如,假设您有一个包含对象列表的简单 Java 类:

package com.example;

import java.util.ArrayList;
import java.util.List;

public class ListBean {

    public List<String> getItems() {
        List<String> list = new ArrayList<String>();
        list.add("Thing1");
        list.add("Thing2");
        list.add("Thing3");
        return list;
    }
}

It doesn't matter how the getItems method constructs the list that it is returning. To display these items in the JSP Page using JSTL you would do the following:

getItems 方法如何构造它返回的列表并不重要。要使用 JSTL 在 JSP 页面中显示这些项目,您需要执行以下操作:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<jsp:useBean id="obj" class="com.example.ListBean" scope="page"/>

<select>
    <c:forEach var="item" items="${obj.items}">
     <option>${item}</option>
    </c:forEach>
</select>
</body>
</html>

Instead of using useBean the items collection used in the forEach loop could also come from the session or a request object.

在 forEach 循环中使用的 items 集合也可以来自会话或请求对象,而不是使用 useBean。

This link also has good advice: http://java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/

这个链接也有很好的建议:http: //java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/

回答by Cesar

First, in your JSP import the class you are trying to use:

首先,在您的 JSP 中导入您尝试使用的类:

<%@ page import="com.mypackage.MyClass" %>

Then you can use that class as you would normally do:

然后您可以像往常一样使用该类:

<%
    MyClass c = new MyClass();
    c.getSomeProperty();
%>

To fill the control, you iterate your array and set the value argument of the option tag:

要填充控件,请迭代数组并设置选项标签的 value 参数:

<select>
    <%while (myList.next()){%>
        <option><%out.print(c.getName());%></option>
    <%}%>
</select>

As you can see, there's mixed Java code and HTML. First it outputs the select tag, then on Java code there's a while loop iterating a list of objects. This could be your ResultSet, an array or some other collection. For each iteration it creates an option tag with some value, this would be the value you want the user to see.

如您所见,Java 代码和 HTML 混合使用。首先它输出 select 标签,然后在 Java 代码中有一个迭代对象列表的 while 循环。这可能是您的ResultSet、数组或其他一些集合。对于每次迭代,它都会创建一个带有某个值的选项标签,这将是您希望用户看到的值。

This is the basic approach, using only JSP. But there are many tag libraries, for example JSTL, that provide things like iteration so you can write things like:

这是基本方法,仅使用 JSP。但是有很多标记库,例如 JSTL,提供了诸如迭代之类的功能,因此您可以编写如下内容:

<select name="mySelect">
    <foreach collection="<%= myCollection %>" var="mybean">
        <%= mybean.getOptionTag() %>
    </foreach>
</select>