繁体   English   中英

jstl遍历对象中的列表

[英]jstl loop through a list in an object

我正在构建一个Spring MVC Web应用程序,我有一个名为NodeRel的对象,其定义如下:

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 
}

在服务器端代码中,我获得了NodeRels的列表,并将其绑定到模型。 在jsp页面中,我想先遍历List,然后在其中,我要遍历List。 我的jsp代码:

<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>

我的代码不起作用,并且出现了java.lang.NumberFormatException:对于第二个循环附近的输入字符串:“ fromNodeSend”:

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

我的代码有什么问题?

请注意,变量${nodeRelInfo}代表列表,变量${nodeRel}代表您处理的每个项目。

因此,您要在第二个循环中循环的项目是${nodeRelInfo.fromNodeSend} 更改变量循环的第二个名称:

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

它与Java for-each循环的逻辑相同。

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

像这样更改第二个循环,因为父循环中的变量名是nodeRel而不是nodeRelInfo

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM