Jsp 使用jstl动态加载下拉列表
我必须通过从数据库获取值来动态加载下拉列表。我使用Servlet作为控制器将数组列表传递到jsp页面。在jsp页面中,我使用jstl显示数组列表,但没有显示值。任何帮助都将不胜感激 DAO:Jsp 使用jstl动态加载下拉列表,jsp,jstl,Jsp,Jstl,我必须通过从数据库获取值来动态加载下拉列表。我使用Servlet作为控制器将数组列表传递到jsp页面。在jsp页面中,我使用jstl显示数组列表,但没有显示值。任何帮助都将不胜感激 DAO: //Method call to retrieve the customer names from Database public List<Report> getAllCustomers() { List<Report> customers = new A
//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);
}
<%@ 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>
}代码似乎正确。。。尝试将
var=“CustomerList”
更改为其他名称
当您在as${CustomerList.customerName}
试试var=“客户”
然后
${customers.customerName}
我认为这主要是因为以下原因:
${CustomerList.customerName}在GET映射上设置它(而不是POST),并将显示在那里 编辑:
(在您的servlet上),doGET方法应该有它,然后将在您的站点上,然后使用post方法可以恢复值。我已经尝试更改“var”属性的值。它不显示数组列表中的值,而是只显示“${CustomerList.customerName}”。我在标签中单独给出的内容是displayedI changed var=“customers”。更改后,我也得到了我在前面的评论中提到的相同结果,您需要将CustomerList更改为新名称,如customers。customerName CustomerList也是您设置的请求属性的名称。。。。我想这就是造成冲突的原因。
private String customerName;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}