0

I am having a array of name list like as shown below

[{"name": "test1"},
{"name": "test2"},
{"name": "test3"},
{"name": "test4"},
{"name": "test5"},
{"name": "test6"},
{"name": "test7"},
{"name": "test8"},
{"name": "test9"},
{"name": "test10"},
{"name": "test11"},
{"name": "test12"},
{"name": "test13"}]

What I am trying to achieve is something like a for loop as shown below but in jstl which prints the four values in each iteration

for(int i=0; i<items.size();i++)
{
    System.out.println(items[i].name);
    System.out.println(items[i++].name);
    System.out.println(items[i++].name);
    System.out.println(items[i++].name);
}

My jstl code is as given below, the code works but increment is not working within the <c:forEach>

<c:forEach var="value" items="${items}" varStatus="loopCounter">
    <div class="one">{value}</div>
    ${loopCounter+1}
    <div class="two">{value}</div>
    ${loopCounter+1}
    <div class="three">{value}</div>
    ${loopCounter+1}
    <div class="four">{value}</div>
</c:forEach>

Can anyone please tell me some solution for this

My expected output is like thsi

Iteration One

<div class="one">test1</div>
<div class="two">test2</div>
<div class="three">test3</div>
<div class="four">test4</div>

Iteration Two

<div class="one">test5</div>
<div class="two">test6</div>
<div class="three">test7</div>
<div class="four">test8</div>

Iteration Three

<div class="one">test9</div>
<div class="two">test10</div>
<div class="three">test11</div>
<div class="four">test12</div>

Iteration Four

<div class="one">test13</div>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Alex Man
  • 4,746
  • 17
  • 93
  • 178

1 Answers1

2

Variable loopCounter(varStatus of your c:forEach tag) is an object containing several properties. One of them is index (zero-based index for the current round of iteration). You can use it to calculate required css class.

If you want to print a variable in a JSP use:

${value}

not

{value}

This should give your desired output:

<c:forEach var="value" items="${items}" varStatus="loopCounter">
    <c:set var="cssClass">
        <c:choose>
            <c:when test="(loopCounter.index % 4) eq 0">
                one
            </c:when>
            <c:when test="(loopCounter.index % 4) eq 1">
                two
            </c:when>
            <c:when test="(loopCounter.index % 4) eq 2">
                three
            </c:when>
            <c:otherwise>
                four
            </c:otherwise>
        </c:choose>
    </c:set>
    <div class="${cssClass}">${value}</div>
</c:forEach>
Keammoort
  • 3,075
  • 15
  • 20
  • Thanks for replying, actually I have four div within the foreach, in a single iteration I need to print four values – Alex Man Dec 17 '15 at 18:29
  • @AlexMan you realize that the generated page will contain the same output(HTML)? My solution will perform `n` iterations (where `n` is size of items collection). You want to have a loop that will do `n/4` iterations, but will get 4 elements in each one. What is the difference? Do you need it for some specific reason to be done in `n/4` iterations? – Keammoort Dec 17 '15 at 18:32
  • I have updated my question with the expected result – Alex Man Dec 17 '15 at 18:35
  • @AlexMan Please see my updated answer. You haven't specified what type of items are in `items` collection - you may need to print value using ${value.name} – Keammoort Dec 17 '15 at 18:42
  • 1
    You need braces for `${cssClass}`. It would probably be more appropriate to use a `` in this case, and you can use the content version of ``, as in: `one...four` – Andreas Dec 17 '15 at 18:50