Dave
Dave

Reputation: 106

Can you make the id dynamic - <div id=[dynamic field here]

I am trying to loop through an Arraylist on my JSP without any luck.

I am able to loop through this successfully but only hit this error while trying to dynamically change the "

I have tried all different approaches by opening and closes the tags, trying different enclosure characters namely: single quotes and double quotes.

The error that I am getting is as follows:

javax.el.MethodNotFoundException: Method not found: class java.util.ArrayList.getChartName()
    at javax.el.Util.findWrapper(Util.java:351)
    at javax.el.Util.findMethod(Util.java:214)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:174)

This what my JSP looks like:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@include file="header.jsp" %>
    <div id="content_main">
        <c:if test="${not empty dashboardDetail}"></c:if>
        <c:forEach var="dashboardDetailList" items="${dashboardDetail}">
            <div id="${dashboardDetail.getChartName()}" style="width: 100%; height: 400px;">    </div>
        </c:forEach>
    </div>
<%@include file="footer.jsp" %>

Upvotes: 0

Views: 1939

Answers (2)

Alex
Alex

Reputation: 11579

Try this:

<c:forEach var="dashboardDetailObject" items="${dashboardDetail}">
   <div id="${dashboardDetailObject.chartName}" 
        style="width: 100%; height: 400px;"></div>
</c:forEach>

dashboardDetailList changed to dashboardDetailObject

Upvotes: 1

James
James

Reputation: 195

From the names you've used, you've got them the wrong way around.

<c:forEach var="dashboardDetailList" items="${dashboardDetail}">

items should be the list and var the current element. But you've referred to dashboardDetail correctly inside the loop. Switch the names (for every reference to the list):

<c:forEach var="dashboardDetail" items="${dashboardDetailList}">

The error occurs because as you have it now, you are calling getChartName() on an ArrayList, and that method doesn't exist.

Upvotes: 2

Related Questions