使用jstl动态加载下拉列表 [英] Load the Drop down list dynamically using jstl

查看:80
本文介绍了使用jstl动态加载下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过使用Servlet作为控制器从Database.Am中获取值来动态加载下拉列表,以将数组列表传递给jsp页面.在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:

公共课报告{

private String customerName;

public String getCustomerName() {
    return customerName;
}

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

}

推荐答案

代码似乎正确...尝试将var="CustomerList"更改为其他名称...

code seems correct... try changing var="CustomerList" to some other name...

问题同名,当您以${CustomerList.customerName}

尝试var ="customers"

try just var="customers"

然后${customers.customerName}

这篇关于使用jstl动态加载下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆