1

I want to create a table with a variable amount of trackers which have a variable amount of trackable devices attached to them. I want to display that in my JSP file. I have tried calling the lists with <c:forEach items="${tracker.getTrackableDeviceList()}" var="trackableDevice">, but I get the error: "Unknown column 't0.trackableDeviceList_ID' in 'where clause'". When I use <td> <c:out value="${tracker.getTrackableDeviceList()}"/> </td> it works but it says the list wasn't instantiated. Which is strange since the trackerList works fine. Last but not least I have tried to create an atribut trackerList for all trackers and an atribut trackableDeviceList for every tracker in trackerList. I wanted to match the atributs with the JSP by using the trackerId, but I've reaceived the error: Stacktrace:] with root cause javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

I found something similar javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach> but it doesn't realy help me (or I just don't get it).

How can I manage to iterate through this? Any kind of advice is appreciated. I cut the error and code down to the relevant parts...

JSP

<form method="post" action="TrackerList_Servelet">  

<table>
    <c:forEach items="${trackerList}" var="tracker"> 
        <tr>      
            <td> <c:out value="${tracker.getPiName()}"/> </td>
            <td> <c:out value="${tracker.getTrackableDeviceList()}"/> </td>
            <c:forEach items="${tracker.getTrackerId()}" var="trackableDevice">   <!-- How to iterate through this? --> 
                <td>
                    <td> <c:out value="${trackableDevice.getId()}"/> </td>
                </td>
            </c:forEach>
        </tr>
    </c:forEach>
</table>

<input type="submit" value="Update"/>
</form>  

Servelet

  @Override  
  protected void doPost(HttpServletRequest request,  
  HttpServletResponse response) throws ServletException, IOException 
{  

    DataManager dataManager = new DataManager();
    dataManager.startDataManagerSetup();     


    //Prepare the trackerList
    List<Tracker> trackerList = dataManager.getTrackerList();
    request.setAttribute("trackerList", trackerList); // Will be available as ${trackerList} in JSP


    //Prepare the trackableDeviceList for every tracker in trackerList
    for(int i = 0; i < trackerList.size(); i++)
    {
        Tracker t = trackerList.get(i);
        List<TrackableDevice> trackableDeviceList = t.getTrackableDeviceList();
        String trackerId = Integer.toString(t.getTrackerId());

        request.setAttribute(trackerId, trackableDeviceList); // Will be available as ${trackableDeviceList} in JSP
    }


    //Answer the request by sending back the list to status.jsp
    request.getRequestDispatcher("status.jsp").forward(request, response);
}
Community
  • 1
  • 1
Fuby
  • 66
  • 10
  • Possible duplicate of [How to iterate over a list of lists in jstl?](http://stackoverflow.com/questions/14737337/how-to-iterate-over-a-list-of-lists-in-jstl) – Ali Seyedi Apr 18 '16 at 22:18

1 Answers1

2

Given that Tracker entity contains a List<TrackableDevice> property, this should do:

List<Tracker> trackerList = dataManager.getTrackerList();
request.setAttribute("trackerList", trackerList);
request.getRequestDispatcher("status.jsp").forward(request, response);

<c:forEach items="${trackerList}" var="tracker">
    <c:out value="${tracker.piName}" />
    <c:forEach items="${tracker.trackableDeviceList}" var="trackableDevice">
        <c:out value="${trackableDevice.id}"/>
    </c:forEach>
</c:forEach>

Yes, indeed just access the property directly. Exactly as you did in the (unnecessary) Java code.


Unrelated to the concrete problem, you'd better place the JSP file in /WEB-INF folder to prevent endusers being able to request it individually without invoking the servlet.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the fast reply. God that looks easy, but in that case the variables need to be public right? Isn't that bad? I`ll try it tomorrow I´m not at home right now. My html and jsp files are currently in the WebContent folder. I guess I'll move them then, thanks. :) – Fuby Apr 18 '16 at 18:46
  • EL just accesses properties via getters. They are public already. Moreover, the presence of the property is not required. It could even have a different name. You don't at all need `${bean.getProperty()}` notation. See also our EL wiki page http://stackoverflow.com/tags/el/info and this answer to a related question: http://stackoverflow.com/a/8577719 – BalusC Apr 18 '16 at 18:47
  • I finally found time to continue my work on this. I've tried your sulution but now I get :com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 't0.trackableDeviceList_ID' in 'where clause' ... I can't seem to post the full error because its to long... – Fuby Apr 20 '16 at 16:52
  • The problem was with the DB connection. BalusC solution worked perfectly fine, thanks. :) – Fuby Apr 20 '16 at 17:45