我必须通过从Database.Am获取值来动态加载下拉列表,使用Servlet作为Controller将Array列表传递给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;
}
Servlet:
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>
豆:
公共课报告{
private String customerName;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
答案 0 :(得分:2)
代码似乎正确...尝试将var="CustomerList"
更改为其他名称......
当您在${CustomerList.customerName}
尝试var =“customers”
然后${customers.customerName}
答案 1 :(得分:1)
我认为这主要是因为以下原因:
&lt;“option value =”$ {CustomerList}“&gt; $ {CustomerList.customerName}&lt;”/“选项&gt;
您可能想要设置
&lt;“option value =”$ {CustomerList.customerID}“&gt; $ {CustomerList.customerName}&lt;”/ option&gt;
假设这是您想要的,或者只是customerName再次
答案 2 :(得分:-1)
在GET映射(而不是POST)上设置它并显示
编辑:
(在您的servlet上),doGET方法应该拥有它,然后将在您的站点上,然后使用post方法可以恢复值。