0

I am trying to populate a JSP table with the contents of a HashMap, but I keep getting PropertyNotFoundExceptions on the first attempt to reference it in the JSP (the firstName property). I have tried using the var['key'] and the var.value.key way of referencing it in EL and every time I get a PropertyNotFoundException. Here's the code I'm using:

The JSP:

<c:forEach var="orders" items="${ orderList }">
            <tr>
                <td class="name">
                    <a href="">${ orders.firstName }</a>
                </td>
                <td>${ orders.date }</td>
                <td>${ orders.product }</td>
                <td>${ orders.comments }</td>
            </tr>
        </c:forEach>

The Servlet that sets the attribute:

FormManager fm = new FormManager();
HashMap hm = fm.getOrder();
request.setAttribute("orderList", hm);

From the FormManager class:

public HashMap getOrder() {
        dbm = new DatabaseManager();
        Statement stmt = null;
        String sql = "SELECT * FROM orders";
        HashMap<String, String> itemMap = new HashMap<String, String>();
        String[] keys = { "firstName", "lastName", "phone", "email" , "date", "product", "comments", "id" };

        dbm.setUrl("jdbc:mysql://localhost:3306/pattycakes");
        dbm.connect();
        try {
            stmt = dbm.getConn().createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();

            int columns = rsmd.getColumnCount();
            for (int i = 0; i < columns; i++) {
                rs.beforeFirst();
                rs.next();
                itemMap.put(keys[i], rs.getString(i + 1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            dbm.disconnect(stmt, dbm.getConn());
        }
        return itemMap;
    }

There may be some issues with that code so far, because I am still in the process of writing this for a family member. All I'm concerned with is how to get past the exception. Thanks!

Patrick S.
  • 275
  • 1
  • 5
  • 19

1 Answers1

0

The JSP seems to assume that the orderList property will be a Collection of maps, but the other code you presented shows it being set as a single map with String keys and String values, representing a single order. Since orderList is a Map, iterating over it via a JSTL foreach yields instances of Map.Entry, one for each entry in the map. Those entries indeed do not have a firstName property, nor any of the other properties you try to extract.

Since the inappropriately-named orderList property really provides data for only one order, your iteration over it does not make sense. I'm inclined to think that you're setting the wrong value for that property, but if it's really what you want then you might display the one order with JSP code along these lines:

<!-- no foreach -->
<tr>
    <td class="name">
        <a href="">${ orderList["firstName"] }</a>
    </td>
    <td>${ orderList["date"] }</td>
    <td>${ orderList["product"] }</td>
    <td>${ orderList["comments"] }</td>
</tr>
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thanks for the help, I knew there were inconsistencies in my code, but didn't see that. It is meant to be a collection of Maps and a table of orders. Big mistake on my part. – Patrick S. Sep 16 '16 at 21:24