de li
de li

Reputation: 932

jstl loop through a list in an object

I am building a Spring MVC web app, I have an object called NodeRel which is defined as below:

public class NodeRel {
    private String fromNodeId;
    private String toNodeId;
    private String fromNodeName;
    private String toNodeName;
    private List<QuotaValueOnTime> fromNodeSend;
    private List<QuotaValueOnTime> toNodeSend;

    //getters and setters omitted 
}

In the server side code, i obtained a list of NodeRels and bind it to the model. In the jsp page, I want to first loop through the List and then inside it, I want to loop though List. My jsp code:

<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th class="center">Count</th>
            <th>relation</th>
            <th colspan='25'>Detail</th>
        </tr>
    </thead>

    <tbody>
        <c:forEach var="nodeRel" items="${nodeRelInfo}" varStatus="stc">
            <tr>
                <td rowspan="3">${stc.count}</td>
                <td rowspan="3">${nodeRel.fromNodeName} --> ${nodeRel.toNodeName}</td>
                <td>\</td>
                <c:forEach var="x" begin="0" end="23" step="1"> 
                            <td>${x}</td>
                </c:forEach>
            </tr>
            <tr>
                <td>Send_A</td>
                <c:forEach var="node" items="${nodeRelInfo.fromNodeSend}"> 
                            <td>${node.sumval}</td>
                </c:forEach>

            </tr>
            <tr>
                <td>Send_B</td>
                <c:forEach var="x" begin="0" end="23" step="1"> 
                            <td>${x}</td>
                </c:forEach>
            </tr>
        </c:forEach>
    </tbody>
</table>
</div>

My code doesn't work and I got java.lang.NumberFormatException: For input string: "fromNodeSend" near the second loop:

<c:forEach var="node" items="${nodeRelInfo.fromNodeSend}"> 
    <td>${node.sumval}</td>
</c:forEach>

What is wrong with my code?

Upvotes: 0

Views: 802

Answers (2)

Nikolas
Nikolas

Reputation: 44496

Note that the variable ${nodeRelInfo} represents the List and the variable ${nodeRel} represents the each item you work with.

Thus the item you want to loop in the second loop is ${nodeRelInfo.fromNodeSend}. Change the second name of variable looped:

<c:forEach var="node" items="${nodeRel.fromNodeSend}"> 
    <td>${node.sumval}</td>
</c:forEach>

It works on the same logic like Java for-each loop.

for (List nodeRel: nodeRelInfo) {
    // bla blaa
    for (String node: nodeRel.fromNodeSend()) {
        System.out.println(node);
    }
}

Upvotes: 2

Jekin Kalariya
Jekin Kalariya

Reputation: 3507

change your second loop like this because your variable name in parent loop is nodeRel not nodeRelInfo

<c:forEach var="node" items="${nodeRel.fromNodeSend}"> 
    <td>${node.sumval}</td>
</c:forEach>

Upvotes: 0

Related Questions