Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I work with Iterables in my JSP pages?

Tags:

java

jsp

jstl

I'm using an API that makes heavy use of Iterables (java.lang.Iterable), but I can't use them directly on my JSP pages because JSTL tags and EL can not process them. Right now, I'm transforming each iterable to a list prior to rendering them.

What would be the cleanest and simplest approach to make it work without previous transformations? Do newest JSTL-Jasper-EL-taglibs-etc. versions support it? Where can I found that information? I don't find anything about it googling...

I know I can use Iterable.iterator(), but I can't call that method inside the JSP, only in my controller class, and this is very limiting.

like image 610
Juan Calero Avatar asked Nov 14 '22 22:11

Juan Calero


1 Answers

To access the iterators of your Iterables in EL expressions in your JSTL tags, you could use Java code (in your model, controller or service layer) that wraps your Iterable objects in instances of a class Iter that looks like this (with a simple getter method that follows the Java beans method name convention):

public class Iter<T> {

    private final Iterable<T> iterable;

    public Iter(Iterable<T> iterable) {
        this.iterable = iterable;
    }

    public Iterator<T> getIterator() {
        return iterable.iterator();
    }
}
like image 157
oldo Avatar answered Dec 09 '22 19:12

oldo