Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in JSTL whether object is String or Collection? [duplicate]

Tags:

java

jstl

I'm using JSTL and want to check whether an object is a String or a Collection.

fn:length returns results on both types (stringsize or number of elements in the collection).

<c:if test="${fn:length(item)>1}">
   <c:out value="${fn:length(item)} " />
</c:if>

How can I determine which one I've got?

like image 684
Hedge Avatar asked Aug 10 '11 16:08

Hedge


2 Answers

You could look at the class name. For example:

<c:if test="${item.class.simpleName == 'String'}">
   <!-- it's a String! -->
</c:if>
like image 154
dogbane Avatar answered Nov 08 '22 02:11

dogbane


item.class lead to errors when using with tomcat 7. For me this works (although it's dirtier):

${item.link.getClass().simpleName == 'String'}
like image 35
Matthias M Avatar answered Nov 08 '22 02:11

Matthias M