Question

I am working on my website and trying to print out portfolio objects from the database. The page where I print them out to has pagination.

I use a script to create a new page when the following tag is used:

<div class="pics">
...
</div>

A maximum of 8 anchor tags only should go inside the above div. The anchor tags aren't anything special, they do use <s:property> tags but that's about it.

In order to achieve this functionality, I've tried the following:

<c:set var="counter" value="0"/>
<s:iterator value="allPortfolio">
    <c:if test="${(counter mod 8) == 0}">
        <div class="pics">
   </c:if>

   <a href="<s:property value="filename">">Download</a>

    <c:if test="${(counter mod 8) == 0}">
        </div>
    </c:if>

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

</s:iterator>

This code, however, creates 11 pages (1 per portfolio object in my database). Where it should create just 2 pages with 8 items on the first and the remaining 3 on the second.

In other words, there should be just 2 of

<div class="pics">
...
</div>

One with 8 anchor tags in and one with 3 anchor tags in.

Instead, this is the code that gets created once the page loads:

 <div class="pics">
     <a href="1ba918625bca57f6e059c2d05393010b04da5ed6.png">Download</a>
 </div>
 <a href="ec49d4ca243a481b7fa819dfea783e0dd8f5a431.jpg">Download</a>   
 <a href="1b4ab5a3bebdf88045ef3818e852185c86e33048.png">Download</a>
 <a href="e12eaed32a97bb78ddd863696ca510d943c4d978.png">Download</a>
 <a href="2b6ee029b3fcef21b0a220a6b14de16206ce588b.png">Download</a>
 <a href="e3e94e7ac6bfb09ca012d30912525848f2d361ed.png">Download</a>
 <a href="f07e24b31fb65e38aa3d4bbe97a24a71c281aae7.png">Download</a>
 <a href="dd41b536ca45642fa9827311498b9480d6df9be8.png">Download</a>
 <div class="pics">
     <a href="a2ad0d4b73daa3311e9f9b16dcd7d9476cd6e748.png">Download</a>
 </div>
 <a href="45fa47125a5dfe3608d6d80fb0a9ba1f5c444264.png">Download</a>
 <a href="ee9b1571aa22e935772792ac363cc95e2a77bdfd.png">Download</a>
Était-ce utile?

La solution

Actually you don't need that counter because Struts2 <s:iterator> tag has status attribute exactly for that. Define it and you can use various properties of it inside iterator. For example index will return current count of iterations starting from 0. The first property is boolean that indicates that it is the first available item in the iteration.

BTW don't forget to close your tags.

<s:iterator value="allPortfolio" status="stat">
  <s:if test="#stat.first">
    <div class="pics">
  </s:if>
  <s:if test="!#stat.first && #stat.index % 8 == 0">
    </div>
    <div class="pics">
  </s:if>

  <a href="<s:property value="filename"/>">Download</a>

  <s:if test="#stat.last">
    </div>
  </s:if>
</s:iterator>

Autres conseils

Using modulus of 8 means that every eight link will be divisible by 8 with 0 remainder. It's not the way to solve your problem.

You'd want to use something like:

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

<s:iterator value="allPortfolio">
  <c:if test="${counter gt 7}">
    <c:set var="counter" value="0" />
  </c:if>

  <c:if test="${counter eq 0}">
      <div class="pics">
  </c:if>

  <a href="<s:property value="filename">">Download</a>

  <c:if test="${counter eq 7}">
      </div>
  </c:if>

  <c:set var="counter" value="${counter+1}" />
</s:iterator>

<c:if test="{counter ne 8}">
  </div>
</c:if>

The last bit is for closing the last div, in case it was left open.

Try this code

<s:set var="countdown" value="0"/>
<s:iterator value="allPortfolio" status="status">

  <s:if test="#status.index % 8 == 0">
    <div class="pics">
    <s:set var="countdown" value="7"/> 
  </s:if>

   <a href="<s:url value='%{filename}'/>">Download</a>

   <s:if test="#countdown == 0 || #status.last">
     </div>
   </s:if>

   <s:set var="countdown" value="%{#countdown-1}"/>
</s:iterator>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top