Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not directly, but you can use the <code>varStatus</code> to put an instance of <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html" rel="nofollow noreferrer"><code>LoopTagStatus</code></a> in the scope of <code>&lt;c:forEach&gt;</code>. It offers several getters to figure among others the loop <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html#getIndex%28%29" rel="nofollow noreferrer">index</a> and whether it's the <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html#isFirst%28%29" rel="nofollow noreferrer">first</a> or the <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html#isLast%28%29" rel="nofollow noreferrer">last</a> iteration of the loop.</p> <p>I'm only unsure how your <code>&lt;c:if&gt;</code> makes sense, but I think that you actually have two lists of the same size with comment names and comment rates and that you need to show only the rate at the same index as the comment.</p> <pre class="lang-html prettyprint-override"><code>&lt;c:forEach items="${commentNames}" var="comment" varStatus="commentLoop"&gt; ${comment} &lt;c:forEach items="${rates}" var="rate" varStatus="rateLoop"&gt; &lt;c:if test="${commentLoop.index == rateLoop.index}"&gt; ${rate} &lt;/c:if&gt; &lt;/c:forEach&gt; &lt;/c:forEach&gt; </code></pre> <p>This is however clumsy. You can better get the rate by index directly.</p> <pre class="lang-html prettyprint-override"><code>&lt;c:forEach items="${commentNames}" var="comment" varStatus="commentLoop"&gt; ${comment} ${rates[commentLoop.index]} &lt;/c:forEach&gt; </code></pre> <p>Much better is to create a <code>Comment</code> object with a <code>name</code> and <code>rate</code> property.</p> <pre><code>public class Comment { private String name; private Integer rate; // Add/autogenerate getters/setters. } </code></pre> <p>So that you can use it as follows:</p> <pre class="lang-html prettyprint-override"><code>&lt;c:forEach items="${comments}" var="comment"&gt; ${comment.name} ${comment.rate} &lt;/c:forEach&gt; </code></pre> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/1727603/places-where-javabeans-are-used">Places where JavaBeans are used?</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload