Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load the Drop down list dynamically using jstl [duplicate]

Tags:

jsp

jstl

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;
}

}

like image 368
Mogana Avatar asked Mar 18 '26 13:03

Mogana


2 Answers

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}

like image 134
AurA Avatar answered Mar 20 '26 07:03

AurA


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

like image 34
ah-shiang han Avatar answered Mar 20 '26 09:03

ah-shiang han



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!