假设当前在gsp中:

<g:each in="${prospectProjectStatuses}" var="statusObj">
    <%-- Number active and inactive projects --%>
    <g:if test="${statusObj?.project?.active == true}">
        <g:set var="numActiveProjects" value="${numActiveProjects + 1}"/>
    </g:if>
    <g:else>
        <g:set var="numInactiveProjects" value="${numInactiveProjects + 1}"/>
    </g:else>

    <%-- Number of hidden rows to show --%>
    <g:if test="${!statusObj?.project?.id.intValue() && !canEdit && statusObj?.status?.id}">
        <g:set var="numProjectsToShow" value="${numProjectsToShow + 1}"/>
    </g:if>

    <g:if test="${statusObj?.project?.active && statusObj?.status?.id}">
        <g:set var="numStatusSet" value="${numStatusSet + 1}"/>
    </g:if>
</g:each>

Controller 方法中的每个循环等效于什么?

最佳答案

在您的集合上调用each()。因此,示例gsp的等效内容如下所示:

prospectProjectStatuses.each { statusObj ->
    if (statusObj?.project?.active) {
        numActiveProjects += 1
    } else {
        numInactiveProjects += 1
    }
    if (statusObj?.project?.id.intValue() && !canEdit && statusObj?.status?.id) {
        numProjectsToShow += 1
    }
    if {statusObj?.project?.active && statusObj?.status?.id) {
        numStatusSet += 1
    }
}

10-06 01:23