wood burning stoves 2.0*
The moose likes JSP and the fly likes Getting the List size in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Getting the List size in JSP" Watch "Getting the List size in JSP" New topic
Author

Getting the List size in JSP

Elizabath Lima
Ranch Hand

Joined: Nov 23, 2005
Posts: 32
Hi,

I would like to get the size of the list in jsp. I need the total size of the list to be displayed in the page. I couldn't yet find ot a solution for this. Is it possible at jsp to get the list size using jstl? or shall i use <% %> to get the size like jsp 1.0?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Is this a List interface ? It does not follow the javabeans convention, so I don't think that you can get the size of the list. A quick way would be to use a scriptlet <%= mylist.size() %>

If you want to avoid sciptlets, a little wrapper around your list, with a getSize() method could do the trick.
Or you could even put the list size in a request atribute.

If you're looping through the list before displaying the total number of records using itertors like c:forEach, you could keep the total count in a page-scoped variable.
[ April 11, 2006: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Elizabath Lima
Ranch Hand

Joined: Nov 23, 2005
Posts: 32
Hi,

Thanks for your reply.

I'm searching a jstl implementation for my jsp code. I'm trying for a jstl and jsp 2.0 implementation for the following code

code

for(int i=offset.intValue(), l = Math.min(i + maxPageItems,arrResult.size()); i < l ;i++) {
MyBean result = (MyBean)arrResult.get(i);
<td><%=result.getCity()%></td>
............................................

if that for loop is omitted, then rest is working fine for me..

<c:forEach items="${searchResults}" var="searchResult" varStatus="searchIndex"> .....

while using the Math.min in the <c ut> its throwing an exception like this

/WEB-INF/pages/Search.jsp(329,14) The function min must be used with a prefix when a default namespace is not specified

How i can convert the above code to jstl and jsp 2.0? and is it possible to assign a scriplet as the value in a jstl tag?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

${fn:length(list)}

You should have a copy of the JSTL Spec on your desk.
[ April 11, 2006: Message edited by: Bear Bibeault ]

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

Joined: Jul 06, 2005
Posts: 1018
This assumes that the values "offset" and "maxPageItems" are available to JSTL, and are not just scriptlet variables.
The call to Math.min has been replaced with a <c:if>

Elizabath Lima
Ranch Hand

Joined: Nov 23, 2005
Posts: 32
Thanks a lot Stefan Evans

You have done a great work for me.

And everything working fine for me except calling a jsp from another jsp in the spring framework.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the List size in JSP