tags:

views:

135

answers:

1

I need to render a 3-level deep list of items via JSP. Let's say I have beans with String properties "Country", "City", "Street". I need to build a heirarchy of UL and LI tags to show streets such that they are listed under the appropriate city, and such that each city is listed under the appropriate country.

Example:

<ul>
<li>United States
    <ul>
    <li>Washingron
            <ul>
        <li>Independence Ave</li>
        <li>23d Street</li>
        </ul>
    </li>
    <li>Detroit
        ...
    </li>
    </ul>
</li>
<li>United Kingdom
...
</li>
</ul>

Obviously, I could use forEach, but properly opening and closing tags for each list would require tons of if statements. Is there some simple way to do this via JSTL?

+1  A: 
<ul>
<c:forEach items="${countriesList}" var="country"> 
   <li>${country.name}
      <ul>
         <c:forEach items="${country.stateList}" var="state">
            <li>${state.name}
                <ul>
                   <c:forEach items="${state.addressLines}" var="addressLine">
                      <li>${addressLine.addressString}</li>
                   </c:forEach>
                </ul>
            </li>
         </c:forEach>
      </ul>
   </li>
</c:forEach>
</ul>
Chii
You're missing "tons of if statements" - specifically, to check whether country has any states and whether state has any address lines. Without those you're printing `<ul></ul>` at best and failing with error (if the collection is null) at worst.
ChssPly76
i dont get why you'd have tonnes of if statements - the data structure to render should have the correct data - your servlet/controller ought to have munged and filtered the correct data out before it goes to the render stage.
Chii
I disagree. View is responsible for rendering itself correctly; neither model nor controller should care.
ChssPly76
@Chii, I would agree with ChssPly76 here.
Adeel Ansari
By the way, I found the post good enough to give an idea. Introducing if statements would be trivial wherever needed.
Adeel Ansari
but the problem with having all those nested if statements is that the view is now concerned with checking for existence of things - that is the domain of the model - the model should perform the business logic, and hand a data object (via the controller) to the view, and all the view will do is figure out how it should render. If you have missing states, the model ought to filter those missing stuff out, so that the data object passed to the view is cleaned up nicely. Otherwise, you end up doing "business logic" in the view. But as with most things, it takes judgement and depends.
Chii
Hm... The code looks reasonable. I'm honestly not sure why I didn't write the same thing on my own. Brain glitch of some sort. Anyway, thanks.
Gambler
Ah, now I remembered the problem I had. I wanted to use Java's own collections (Maps) without writing my own.Initially, I have a bunch of street objects, which have properties "name", "city" and "country." I pass a set of those to JSP, and it should render them as a 3-level list. I could call a grouping method, which returns a Map of Maps of Maps, but maps don't have any place to store names. Writing custom containers would solve the problem, but that doesn't seem like a particularly good design.
Gambler
@gambler: you can refer to a map's key in jsp easily like ${map['key_name']}, e.g., ${country['street']['name']} or something like that.
Chii