Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the size of a collection in JSP/JSTL/EL [duplicate]

Tags:

java

jsp

jstl

el

I have a List variable called services in my JSP page. I need to add some markup to the page if there's more than 1 element in the list.

What I'd like to do is...

<c:if test="${services.size() gt 1}">   <!-- markup... --> </c:if> 

But you can't invoke methods on Java objects in EL (I think this is perhaps the 364823782 time I've regretted that fact). You can only access getters on Java objects by dropping the 'get,' e.g. ${user.name} for a User class that has a getName() method.

What's the right way to evaluate this test?

like image 876
Drew Wills Avatar asked Aug 26 '10 21:08

Drew Wills


People also ask

How do I find the length of an array in JSTL?

JSTL - fn:length() Function The fn:length() function returns the string length or the number of items in a collection.

What are the tags in JSP?

A tag file is a source file that contains a fragment of JSP code that is reusable as a custom tag. Tag files allow you to create custom tags using JSP syntax. Just as a JSP page gets translated into a servlet class and then compiled, a tag file gets translated into a tag handler and then compiled.

What is the full form of JSP Java Servlet program Java Server Pages Java Server ports Java Server program?

JavaServer Pages (JSP) is a complimentary technology to Java Servlet which facilitates the mixing of dynamic and static web contents.


1 Answers

You are looking for fn:length(services). Remember to define the fn namespace.

http://download.oracle.com/javaee/5/tutorial/doc/bnalg.html

like image 124
Thorbjørn Ravn Andersen Avatar answered Sep 30 '22 06:09

Thorbjørn Ravn Andersen