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

旧时模样 提交于 2020-01-01 09:44:05

问题


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?


回答1:


You could look at the class name. For example:

<c:if test="${item.class.simpleName == 'String'}">
   <!-- it's a String! -->
</c:if>



回答2:


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

${item.link.getClass().simpleName == 'String'}


来源:https://stackoverflow.com/questions/7013973/how-to-check-in-jstl-whether-object-is-string-or-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!