tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig Longman <>
Subject Re: How to break a loop in JSTL c:foreach
Date Thu, 26 Sep 2002 07:47:36 GMT
On Thu, 2002-09-26 at 02:28, Eric Du wrote:
> I think breaking a loop is a general and useful feature for better 
> performance.


> Here is a case:
> Assuming a multiple select control in a form, and a java variable 
> id2textMap keeps all the items showed in the select control, each entry 
> in the map is responding to an item in the control and "id" is the value 
> and "text" is the text. A variable selIDs keeps all selected items. Now 
> the jstl codes to generate the select control is as follows:
> <select name="selIDs" size="5" multiple>
> <c:foreach var="entry" items="id2textMap">
>    <option value="<c:out value="${entry.key}"/>"
>      <c:foreach var="selID" items="selIDs">
>        <c:if test={selID==entry.key}>
>          selected
>          <%-- I WANT TO BREAK THE LOOP HERE -->
>        </c:if>
>      </c:foreach>
>    >
>    <c:out value="${entry.value}"/>
>    </option>
> </c:foreach>
> </select>
> In our project, we have a page that contains more than 50 select 
> controls like that and the performance to display that page with JSTL is 
> not good.

in this case, i think the best solution would be to make the selIDs also
a Map.  then your code gets a) simplier, b) much quicker regardless of
the size of the lists or the selected items.  you would have:

<c:foreach var="entry" items="id2textMap">
 <option value="<c:out value="${entry.key}"/>"
  <c:if test="${not empty selIDs[entry.key]>selected</c:if>>
 <c:out value="${entry.value}"/></option>

i might have typed something in not quite right, but it should work
pretty close to this.

i don't necessarily stay way from breaking loops as a religion, but i do
find that 9 times out of 10 there is a better way.

> Shawn Bayern wrote:
>  >>Would it be possible to have an loop tag which iterates based on a
>  >>boolean condition?
>  > A <c:while>?  Yes, additions like this are always possible in future
>  > versions if there's enough perceived need.
>  >
>  > I'd suggest mailing with the suggestion.  Be sure
>  > to indicate the situations where this would be useful to you, with as
>  > specific examples as possible.
>  >
>  > Best,
>  >
> --
> To unsubscribe, e-mail:   <>
> For additional commands, e-mail: <>

    Be Developer ID: 5852

View raw message