I am trying to use it inside several tags I am trying to use it inside several tags I am trying to use it inside several tags

How to pass an attribute from JSP to tag file. Override the value in tag, and get the updated value in JSP

886 Views Asked by At

I have a JSP where I am declaring an integer value

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

I am trying to use it inside several tags

<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}" />

Inside each tag I am overriding the rowCount value

<%@ 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}" />

But it seems I am declaring a new variable for rowCount in each tag. How do I reuse the value instead of declaring a new variable?

1

There are 1 best solutions below

0
Ricardo Machado On

I was trying with adding the scope for only the first declaration of the variable and it was not working.

Then I tried to add for them all, like this:

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

and

<%@ 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"/>

It worked partially. I was able to add new values to the row count, but the line

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

Was not working. It never went back to zero. So I change it to have a local variable and override the value only once. like this:

<%@ 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"/>

Now I have the result that I want. But I still could not understand why I could not override it twice.