简体   繁体   English

JSTL打破循环

[英]JSTL Break out of loop

I am trying to retrieve 20 records from my list of friends with their name starting with 'J'. 我正在尝试从我的朋友列表中检索20条记录,其名称以“ J”开头。 However, I realized there is no break in JSTL. 但是,我意识到JSTL没有中断。 So I tried to use a count to keep track of the records. 因此,我尝试使用计数来跟踪记录。 I hit into error 'PWC6031: Unterminated <c:set tag' 我遇到错误“ PWC6031:未终止的<c:set标签”

<c:set var="count" value="0" scope="page">
    <c:forEach items="${friendsList}" var="user">
        <c:if test="${count < 20}">
            <c:set var="string1" value="${fn:substring(user.name, 0, 1)}" />
            <c:if test="${fn:startsWith(string1,'J')}">
                <tr>
                <td><c:out value="${user.name}" /></td>
                <td><img src='https://graph.facebook.com/<c:out value="${user.id}"/>/picture' />
                </td>
            </tr>
            </c:if>
        </c:if>
        <c:set var="count" value="${count + 1}" scope="page"/>
    </c:forEach>

Is there a way that I can retrieve the value from count variable? 有没有一种方法可以从count变量中检索值? If not, is there an alternative way that I can break out of the loop to achieve what I want? 如果不是,是否有其他方法可以使我摆脱困境,实现自己想要的目标? Thanks. 谢谢。

After resolving the error that I hit, I managed to make use of the count variable to list what I wanted. 解决了我遇到的错误后,我设法利用count变量列出了我想要的内容。 I mixed up the ordering of my 'count' coding in my original coding. 我在原始编码中混合了“计数”编码的顺序。 Here's what I have now, is working, thanks everyone for the helps! 这是我现在正在工作的内容,感谢大家的帮助!

<c:set var="count" value="0" scope="page" />
    <c:forEach items="${friendsList}" var="user">
        <c:if test="${count < 20}">
            <c:set var="string1" value="${fn:substring(user.name, 0, 1)}" />
            <c:if test="${fn:startsWith(string1,'J')}">
                <tr>
                    <td><c:out value="${user.name}" /></td>
                    <td><img src='https://graph.facebook.com/<c:out value="${user.id}"/>/picture' /></td>
                </tr>
                <c:set var="count" value="${count + 1}" scope="page" />
            </c:if>
        </c:if>
    </c:forEach>

You're making it more difficult than necessary. 您正在使它变得不必要的困难。 Here are alternative solutions that are all better than yours, IMO, from the worst to the best one: 从最坏的情况到最好的情况,以下是比您的IMO都更好的替代解决方案:

  1. Use the varStatus="loopStatus" attribute of c:forEach which allows knowing the index of the iteration inside the loop using ${loopStatus.index} . 使用c:forEachvarStatus="loopStatus"属性,该属性允许使用${loopStatus.index}知道循环内迭代的索引。 This allows removing your count attribute. 这样可以删除您的count属性。
  2. Use the end="19" attribute of c:forEach 使用c:forEachend="19"属性
  3. Prepare the list in your controller (or even in your SQL query, when retrieving the users) to make sure it has only 20 elements max: 在控制器中(或在检索用户时甚至在SQL查询中)准备列表,以确保列表最多只有20个元素:

     users = users.subList(0, Math.min(users.size(), 20)); 

The issue seems to be with your first line. 问题似乎与您的第一行有关。 You have not terminated the c:set tag. 您尚未终止c:set标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM