Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the value of a List.size() in a JSP page? [duplicate]

Tags:

java

jsp

This is a simple question, I should know the answer and I'm ashamed to admit I don't. I am sending a compound object to my JSP page:

public class MyObject {
    private List<MyFoo> myFoo;
    public getMyFoo();
    public setMyFoo();
}

// In the controller...
model.addAttribute("myObject", myObject);

When this goes into the JSP page I can address the model instance of myObject as:

${myObject}

and the list inside as

${myObject.myFoo}

What I want to do is list the size of myFoo on my JSP output, like so:

${myObject.myFoo.size}

But of course size() is not a bean property, so a JSPException is thrown.

Is there a simple, elegant way of doing this in the JSP page or do I need to stick another attribute on the model and put the size in there?

like image 982
user1071914 Avatar asked Nov 30 '15 22:11

user1071914


1 Answers

You could use JSTL tag libraries, they are often useful for common JSP operations. Here's a quick reference: https://code.google.com/p/dlcode/downloads/detail?name=jstl-quick-reference.pdf

Include the taglib with this line:

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

Then your case would be:

${fn:length(myObject.myFoo)}
like image 94
Matteo Di Napoli Avatar answered Nov 15 '22 06:11

Matteo Di Napoli