0

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:

  • GENERAL EDUCATION
    • DEPARTMENT OF GENERAL EDUCATION
  • SCHOOL OF BUSINESS
    • BSBA - FINANCIAL MANAGEMENT
    • BSBA - MARKETING AND ADVERTISING

... and so on.

Roman C
  • 49,761
  • 33
  • 66
  • 176
k_rollo
  • 5,304
  • 16
  • 63
  • 95

2 Answers2

2

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;
    //...
}
skuntsel
  • 11,624
  • 11
  • 44
  • 67
0

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

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • I replaced my code with your snippet and this is what happend - [click](http://s18.postimg.org/c92ozi3jd/output2.png). – k_rollo Feb 10 '15 at 14:44
  • way of populating the list in server side seems to be little clumpsy, above is the syntax to iterate the `list of lists` . i advise you to use `Map>` for your case – Santhosh Feb 10 '15 at 14:47
  • 1
    Hi, I have not used a `Map` before, could you kindly post how the Action Class would look like? – k_rollo Feb 10 '15 at 14:54
  • 2
    @SanKrish I was not the one who downvoted and I don't intend to do that, but I'll explain the downvote. Your initial answer didn't take into account that most possibly `school` is in fact a `String` and not a `List` thus *this part should be wrong*. And your update doesn't answer the initial question but instead proposes a brand new solution so *it answers a different question*. – skuntsel Feb 10 '15 at 15:14