gpt4 book ai didi

jsp - 使用 jSTL 动态加载下拉列表

转载 作者:行者123 更新时间:2023-12-02 01:58:03 25 4
gpt4 key购买 nike

<分区>

我必须通过从数据库中获取值来动态加载下拉列表。我使用 Servlet 作为 Controller 将数组列表传递到 jsp 页面。在 jsp 页面中,我使用 jSTL 显示数组列表,但未显示值。任何帮助将不胜感激。

DAO:

//Method call to retrieve the customer names from Database        
public List<Report> getAllCustomers() {

List<Report> customers = new ArrayList<Report>();

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try {



prop = PropertyFileLoaderTon.getInstance()
.getPropertiesConfiguration(REPORTDATA_PROPERTY_FILE);

String tableName = prop.getString(REPORTS_TABLE);

String sql = "select distinct CUSTOMERNAME from tableName ";


conn = ConnectionFactory.getInstance().getConnection();


stmt = conn.createStatement();

rs = stmt.executeQuery(sql);

while (rs.next()) {
Report report = new Report();

String customer = rs.getString("CUSTOMERNAME");

report.setCustomerName(customer);


customers.add(report);

}

} catch (Exception e) {

e.printStackTrace();
} finally {

try {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
}

}
return customers;
}

小服务程序:

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

GenericDao genericDao = new GenericDao();

List<Report> customers = genericDao.getAllCustomers();

request.setAttribute("CustomerList", customers);

request.getRequestDispatcher("jsp/ShowReport.jsp").forward(request,
response);

}

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

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


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/ReportData/DisplayReport" method="post">

Please select an element:


<select id="selectedRecord" name="selectedRecord">

<c:forEach var="CustomerList" items="${CustomerList}">

<option value="${CustomerList}">${CustomerList.customerName}</option>

</c:forEach>

</select>

<input type="submit" value="Submit" align="middle">

</form>

</body>
</html>

bean 类:

公开课报告{

private String customerName;

public String getCustomerName() {
return customerName;
}

public void setCustomerName(String customerName) {
this.customerName = customerName;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com