aspose file tools*
The moose likes JSP and the fly likes accessing size of an arraylist using tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "accessing size of an arraylist using tag" Watch "accessing size of an arraylist using tag" New topic
Author

accessing size of an arraylist using tag

Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
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

Joined: Sep 13, 2005
Posts: 238
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

That assumes that listSize has been set up a scoped variable prior to the JSP gaining control and is not a general solution.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027
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

Joined: Sep 13, 2005
Posts: 238
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

Is your web.xml set up properly as outlined in the JSP FAQ?
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Sep 13, 2005
Posts: 238
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing size of an arraylist using tag