gotch4
gotch4

Reputation: 13259

JSP tags and variable scope

Suppose I have a JSP file that contains a tag (hence a .tag file).

It contains only this code (I omitted the obvious <%@ tag... ):

<div id = "***" class = "mySpecialClass" >Test</div>

how do I substitute * with a progressive id that will be incremented from 0 for the page that contains one or more times that tag, but will restart from 0 on a page reload?

Upvotes: 0

Views: 1578

Answers (2)

JB Nizet
JB Nizet

Reputation: 691635

<c:if test="${empty requestScope.tagCounter}">
    <c:set var="tagCounter" scope="request" value="${0}"/>
</c:if>
<div id="${requestScope.tagCounter}" class="mySpecialClass">Test</div>
<c:set var="tagCounter" scope="request" value="${tagCounter + 1}"/>

Upvotes: 1

Pat-rice
Pat-rice

Reputation: 432

I'm not sure of what you mean but if you mean generate tags automatically you can do something like this :

                    <div class="tags">
                            <%
                                    for (int i = 0; i < 10; i++) {
                            %>

                            <div id="<%=i%>" class="myspecialclass">Test</div>

                            <%
                                    }//for
                            %>
                    </div>

otherwise can you please give more info ?

Upvotes: 0

Related Questions