k_rollo
k_rollo

Reputation: 5472

Looping through a list of lists using JSTL

I have a list of courses depending on school code. This list of courses is added to another list, making it a list-of-lists.

Action Class:

private List<String> schoolList;
private List<String> socList;
private List<String> sobList;
private List<String> sodList;
private List<String> genlist;
private List<List<String>> courseList = new ArrayList<List<String>>();

@Override
public String execute() {
    FacultyManager fm = new FacultyManager();
    schoolList = fm.getColumn("school_description", "school");

    genlist = fm.getCoursesBySchoolCode("GEN");
    sobList = fm.getCoursesBySchoolCode("SOB");
    socList = fm.getCoursesBySchoolCode("SOC");
    sodList = fm.getCoursesBySchoolCode("SOD");     

    courseList.add(genlist);
    courseList.add(sobList);
    courseList.add(socList);        
    courseList.add(sodList);        

    return SUCCESS;
}

JSP:

<c:forEach var="school" items="${schoolList}" varStatus="ctr">
    <ul>
        <li>${school}
            <ul>
                <c:forEach var="course" items="${courseList}">
                    <li>${course}</li>
                </c:forEach>
            </ul>
        </li>
    </ul>
</c:forEach>

Output enter image description here

How do I make it so the output is:

... and so on.

Upvotes: 0

Views: 2575

Answers (2)

skuntsel
skuntsel

Reputation: 11742

You are iterating the whole list of courses in all schools over and over again, thus repeating the same output as if you called courseList.toString() method multiple times.

You should instead iterate over a concrete list of courses in a current school, which most likely (but nowhere stated in your question) depends on the current iteration index of the outer <c:forEach> loop. This is in turn captured in the index property of the exported ctr variable (that is zero-based) of the school list iteration index.

Thus, you should iterate over a specific list of the courseList list, depending on the current school. You can do this by calling courseList.get(ctr.index), assuming you are on EL2.2+.

This all yields:

<c:forEach var="school" items="${schoolList}" varStatus="ctr">
    <ul>
        <li>${school}
            <ul>
                <c:forEach var="course" items="${courseList.get(ctr.index)}">
                    <li>${course}</li>
                </c:forEach>
            </ul>
        </li>
    </ul>
</c:forEach>

That said, List<List<String>> does not sound as a good model choice. For instance, if you change insertion order you'll get the wrong courses in your view. Map<String, List<String>> construct fits better your domain model as it can be used to map school name to a list of course names unambiguously.

Another option to consider seriously is the usage of objects (you are doing OOP in the end). You should turn away from plain strings and move into objects domain. To start, investigate the following class hierarchy:

public class Course {
    //...
    private School school;
    //...
}

public class School {
    //...
    private List<Course> courses;
    //...
}

Upvotes: 2

Santhosh
Santhosh

Reputation: 8197

If you are trying to iterate the list inside the list , you can achive it by

<c:forEach var="school" items="${schoolList}" varStatus="ctr">
    <ul>
        <li>${school}
            <ul>
                <c:forEach var="course" items="${school}">
                    <li>${course}</li>
                </c:forEach>
            </ul>
        </li>
    </ul>
</c:forEach>

You need to iterate the inner list from the list you pass it to the jsp.

Update

In the case if you are using Map<String, List<String>> , you can iterate it as

 <c:forEach var="school" items="${schoolList}" varStatus="ctr">
        <ul>
            <li>${school.key}
                <ul>
                    <c:forEach var="course" items="${school.value}">
                        <li>${course}</li>
                    </c:forEach>
                </ul>
            </li>
        </ul>
    </c:forEach>

You can use the key and value to get the values from the Map

Upvotes: 0

Related Questions