0

I've got a Map object in my session with a UDO (User Defined Object) as a key and a list of that same object as value. I'm trying to iterate through this map using jstl and printing it as navbar, but apparently it's like the jstl code is not going through pieces of code as if the variables were not correctly set. The problem is that the navbar doesn't get printed at all, and specifically it doesn't even get into the "include subMenu.jsp" part.

Edit: Apparently the problem is with the c:if that just doesn't let the code get in the if clause although the "menu" attribute in that specific moment isn't null! So probably I'm writing the if clause wrong... but where and how?

This is my code:

     <nav role="navigation">
        <ul>
            <c:forEach items="${sessionScope.menuNav}" var="menu">
                <c:if test="${menu.key != null}">
                    <c:if test="${menu.value != null}">
                        <li><a href="#"><c:out value="${menu.key.getTitle()}"></c:out></a>
                            <ul class="dropdown">
                                <c:set var="subMenu" value="${menu.value}" scope="request"/>
                                <jsp:include page="subMenu.jsp"/>
                            </ul>
                        </li>
                    </c:if>
                </c:if>
            </c:forEach>
        </ul>
    </nav>

This is the subMenu.jsp

<c:forEach items="${subMenu}" var="subMenu">
    <c:if test="${subMenu.subMenu == null}">
        <li><a href="${subMenu.getPage()}"><c:out value="${subMenu.getTitle()}"></c:out></a></li>
    </c:if>
    <c:if test="${subMenu.subMenu != null}">
        <li><a href="#"><c:out value="${subMenu.getTitle()}"></c:out></a>
            <ul class="dropdown-submenu">
                <c:set var="subMenu" value="${subMenu.getSubMenu()}" scope="request"/>
                <jsp:include page="subMenu.jsp"/>
            </ul>
        </li>                   
    </c:if>
</c:forEach>

I perfectly manage to put the mapObejct in session. I know because I've run through every single step in debug mode. But I still don't get what part I'm getting wrong. The object is conventionally built (with correctly named getters and setters etc.). This is what I see in debug mode:

enter image description here

The attribute of the session is correctly loaded... I've tried to look for some solutions around but nothing that really helped me... What am I getting wrong?

Edit: This is my Map definition:

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

This is my InvoiceModel Object:

public class InvoiceModel {

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

    //Getters and setters
}
L_Cleo
  • 1,073
  • 1
  • 10
  • 26
  • What exactly goes wrong? – Ward Dec 16 '19 at 14:34
  • The navbar is actually empty, nothing gets printed at all – L_Cleo Dec 16 '19 at 14:35
  • I would suggest to make use a `.tag` file instead of a `.jsp` file for the submenu. In that case, the `subMenu.tag` file could have a `subMenu` _attribute_ that can be passed... – Ward Dec 16 '19 at 14:36
  • So instead of ``, you would then have `` (where _xx_ is the namespace of your taglib) – Ward Dec 16 '19 at 14:37
  • And then on the other page I'll just use it just as if it were a session attribute? – L_Cleo Dec 16 '19 at 14:41
  • Problem is that with that .jsp I'm doing a recursion to print all the children and granchildren and so on – L_Cleo Dec 16 '19 at 14:47
  • 1
    Your problem description is very ambiguous. To begin, are you saying that this minimal snippet `${menu}` already doesn't print anything in first place? – BalusC Dec 16 '19 at 15:56
  • @BalusC Yes so it is not getting in the if clause, I've added a little bit more detail on the edit of my question. Perhaps it is not menu.key? but something like getKey() or similar? I thought I got it right because around the internet this is the way I found to call the key of a map in JSTL – L_Cleo Dec 17 '19 at 10:40

0 Answers0