Solved

How to iterate over a collection backwards using c:foreach in JSTL

Posted on 2008-09-29
7
4,332 Views
Last Modified: 2013-11-24
I have an array of strings that I need to list out backwards. For example:
List...
AAAA
BBBB
CCCC
nnnn

I'd like to c:out this as...
nnnn
BBBB
CCCC
AAAA

Where nnnn is the last string in the array.

This is an array (collection) so I'll never know the end - meaning it can't be a fixed start and end. My thinking was to get the array length and use that value minus 1. Is this possible? If so, what is the variable name of the actual length of an array in JSTL so I know where to subtract from?

Here is my current forEach...

<c:forEach var="trailCat" items="${thisCategory.crumbTrail}" varStatus="status">

That will display
AAAA
BBBB
CCCC
nnnn
with no problem.

Thanks,
biglarrrr
0
Comment
Question by:biglarrrr
  • 5
  • 2
7 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 22601308
Try this.  
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
  String[] arr = {"AAAA","BBBB","CCCC","DDDD","nnnn"};
  request.setAttribute("myArray", arr);
%>


             ${myArray[arrayLength - status.count]}
0
 
LVL 27

Expert Comment

by:rrz
ID: 22601335
If you want each element on a separate line, then add    inside loop.
0
 
LVL 27

Expert Comment

by:rrz
ID: 22601342
I typed
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:rrz
ID: 22601348

<br/>

Open in new window

0
 

Author Comment

by:biglarrrr
ID: 22601866
Thanks rrz@871311.

I tried using
<%
  String[] arr = {thisCategory.crumbTrail};
  request.setAttribute("myArray", arr);
%>
<c:forEach items="${myArray}" varStatus="status">
             <c:out value="${myArray[arrayLength - status.count]}"/>
</c:forEach>

but that didn't work. I didn't get any results back.
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 22602096
Please try this.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>



             ${myArray[arrayLength - status.count]}
0
 

Author Closing Comment

by:biglarrrr
ID: 31501261
Thanks rrz@871311! I really appreciate your help.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroFront challenge 7 71
MaxSpan challenge 9 67
java continue statement 10 74
allStar challenge 1 43
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now