问题
I have to dynamically load the Drop down list by fetching the values from the Database.Am using Servlet as the Controller to pass the Array list to a jsp page. In jsp page am using jstl to display the array list but the values were not displayed. Any help will be appreciated.
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>
Bean:
public class Report {
private String customerName;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
回答1:
code seems correct... try changing var="CustomerList"
to some other name...
The issue is same name might create a confusion when you access it under as ${CustomerList.customerName}
try just var="customers"
then ${customers.customerName}
回答2:
i assume it's mostly because of the following:
<"option value="${CustomerList}">${CustomerList.customerName}<"/"option>
you might want to set
<"option value="${CustomerList.customerID}">${CustomerList.customerName}<"/option>
assume that's what you want, or simply customerName again
回答3:
Set it on your GET mapping (instead of POST) and will show there
Edit:
(On your servlet), doGET method should have it, then will be on your site, then using post method you can recover the values.
来源:https://stackoverflow.com/questions/18740616/load-the-drop-down-list-dynamically-using-jstl