Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing size of an arraylist using tag

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an arraylist called "searchResults" in my jsp. I can write

<% if(searchResults.size() > 0) { %>
...do my html work
<% } %>

what would be an equivalent scriptless code. I can use either struts or jsp tags.
Thanks
Imad
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found it. its this

<c:if test="${listSize > 0}">

and include

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That assumes that listSize has been set up a scoped variable prior to the JSP gaining control and is not a general solution.
 
Stefan Evans
Bartender
Posts: 1691
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The general solution that works in JSTL1.0 and JSTL1.1 would be using the keyword "empty"



JSTL1.1 introduced User functions, which fixed one of the major issues with JSTL - getting the length of a list.
If you have JSP2.0 / JSTL1.1, the length of a list can be found with the length function.
eg: ${fn:length(searchResults)}"
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. For some reason EL doesn't seem to be working. when I write <c:if test="${not empty searchResults}">....my code</c:if> ...code never goes inside. and when I added the following code outside <c:if.../> it printed in the screen as it is. that is

${searchResults.size} and also ${fn:length(searchResults} ..to use fn i did inculde the following line in my jsp

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

I am using jstl 1.1 and tomcat 5.5
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your web.xml set up properly as outlined in the JSP FAQ?
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Thats probably the reason why its not working. but when i tried declaring it the way in FAQ it gave me a red mark in web.xml and a very long message which wont go away even if i change it back to how it is right now. is it a known problem?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there should be no problem if you specify the web.xml correctly.

What do you mnean by a "red mark"? In an IDE? If so, who cares... does it work when you deploy the application?
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Bear. You are right if there is a red mark in IDE who cares and yes the red mark was in the ide. I was just afraid if it will going to show on everyone elses machine when i check back in the web.xml but I hope it dont. anyways its working fine.

Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic