0

我有一个 JSP,我在其中声明一个整数值

<c:set var="rowCount" value="0" />

我试图在几个标签中使用它

<product:attribute rowCount="${rowCount}" attrKey="${msg_site}&#58;" attrValue="${product.site}" />
<product:attribute rowCount="${rowCount}" attrKey="${msg_name}&#58;" attrValue="${product.name}" />
<product:attribute rowCount="${rowCount}" attrKey="${msg_type}&#58;" attrValue="${product.type}" />

在每个标签内,我都覆盖了 rowCount 值

<%@ attribute name="rowCount"  required="true" %>
<c:if test="${rowCount >= 2}" >
    </div>
    <div class="col-md-12 col-sm-12 col-xs-12 ">
    <c:set var="rowCount" value="${0}" />
</c:if>

<c:set var="rowCount" value="${rowCount +1}" />

但似乎我在每个标签中为 rowCount 声明了一个新变量。如何重用该值而不是声明一个新变量?

4

1 回答 1

0

我试图只为变量的第一个声明添加范围,但它不起作用。

然后我尝试为它们全部添加,如下所示:

<c:set var="rowCount" value="0" scope="request" />

<%@ attribute name="rowCount"  required="true" %>
<c:if test="${rowCount ge 2}" >
    </div>
    <div class="col-md-12 col-sm-12 col-xs-12 ">
    <c:set var="rowCount" value="${0}" scope="request" />
</c:if>

<c:set var="rowCount" value="${rowCount +1}" scope="request"/>

它部分工作。我能够向行数添加新值,但是行

    <c:set var="rowCount" value="${0}" scope="request" />

没有工作。它从未回到零。因此,我将其更改为具有局部变量并仅覆盖该值一次。像这样:

<%@ attribute name="rowCount"  required="true" %>

<c:set var="rowCountInstance" value="${rowCount}" />

<c:if test="${rowCountInstance ge 2}" >
    </div>
    <div class="col-md-12 col-sm-12 col-xs-12 ">
    <c:set var="rowCountInstance" value="${0}" />
</c:if>

<c:set var="rowCount" value="${rowCountInstance +1}" scope="request"/>

现在我得到了我想要的结果。但我仍然不明白为什么我不能两次覆盖它。

于 2020-03-19T12:16:14.520 回答