Ben
Ben

Reputation: 62444

Trouble determining list size in jsp

I'm trying to determine the size of a collection within a view. I'd like to not have to pass the size like I am in the code below with .size().

@RequestMapping(value = "/search/searchAndReplace")
public ModelAndView searchAndReplace(HttpServletRequest request, HttpServletResponse response) {
    ModelAndView model = new ModelAndView("content/search/searchAndReplace");

    List<Content> searchResults = (List<Content>) request.getSession().getAttribute("searchResults");
    model.addObject("searchResults", searchResults);
    model.addObject("searchResultsCount", searchResults.size());

    return model;
}

Within my view, when I do ${searchResultsCount} things work fine, when I do ${searchResults.size()} I get the error Class javax.el.BeanELResolver can not access a member of class java.util.ArrayList$SubList with modifiers "public"

I can't figure out why this is choking. Can someone offer some insight?

Upvotes: 1

Views: 2848

Answers (2)

ChrisThompson
ChrisThompson

Reputation: 2008

I am pretty sure you need to cast it into it's object type first.

<%
List<Content> searchResults = (List<Content>)request.getAttribute("searchResults");
%>

Than you should be able to use ${searchResults.size()}

Upvotes: 2

mthmulders
mthmulders

Reputation: 9705

In your example, the EL parser seeks a size property on the object, but there's no method getSize() on the object. Instead, use the EL-expression fn:length(searchResults). You'll need to define the fn namespace for that:

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

Upvotes: 5

Related Questions