0

I'm trying to iterate through a Map using JSTL but I just can't get it to work. So here's my Map definition:

Map<InvoiceModel, List<InvoiceModel>> menuMap =
        new HashMap<InvoiceModel, List<InvoiceModel>>();

And here is my InvoiceModel class:

public class InvoiceModel {

    private String title;
    private String page;
    private List<String> roles;
    private List<InvoiceModel> subMenu;

    //Getters and setters
}

This is the part of code not working in my Jsp:

 <nav role="navigation">
        <ul>
            <c:forEach items="${sessionScope.menuNav}" var="menu">

                <c:out value="${menu.key}"></c:out>

                //rest of the code
            </c:forEach>
        </ul>
    </nav>

Problem is I've tried pretty much anything on Stack and nothing worked for me... I know for sure that JSTL is working since the first c:forEach actually defines the variable 'menu' inside my page running it in debug (and it isn't empty). 'menuNav' isn't empty either.

The problem arises when I get to the c:out that I'm using for testing what's the problem (this means it extends to the rest of the code, specifically it won't even get in the if code). Apparently instead of printing the variable value it literally prints what's inside the commas so in this case: '${menu.key}'

It should print "Navigate" as it its the title of the Map's key that is iterating in that moment, and I see the actual menu variable in every one of those loops is not null so there is a key with a title property. But also in the case there wasn't there should have been at least an error but...

This is what I've tried:

  • ${menu.key}: and it prints '${menu.key}' on my page instead of "Navigate"
  • ${menu.getKey()}: and it doesn't compile
  • ${menu['key']}: and it prints '${menu['key']}'
  • ${menu.getKey}: and it doesn't compile
  • ${menu['key'].title}: and it prints '${menu['key'].title}'
  • ${menu.key.title}: and it prints '${menu.key.title}'

Edit: I've redone these tests also without c:out as suggested, but still the same thing, still printing just what's written inside the commas and not the value of the expression.

So I don't know how but all of these methods seem to work at least to a few people, bu to me non of them helped... What could be possibly be wrong? I'm getting desperate since it's all day that I'm having this problem and don't know how to solve it

L_Cleo
  • 1,073
  • 1
  • 10
  • 26
  • I'm not sure I understand where exactly you're seeing this problem (or why you're using `` at all). `${menu.key.title}` is sufficient; if it's not working then something else is at play. – Dave Newton Dec 17 '19 at 13:56
  • Well the c:out should work anyways right? – L_Cleo Dec 17 '19 at 14:06
  • I've tried without c:out anyways but it isn't working – L_Cleo Dec 17 '19 at 14:11
  • FWIW those aren't commas, they're quotes. In any case, if your EL isn't being evaluated, that's a separate issue as BalusC has pointed out. – Dave Newton Dec 17 '19 at 15:08

0 Answers0