Get List.size () value on JSP page?

This is a simple question, I must know the answer, and I am ashamed to admit that no. I submit the composite object to the JSP page:

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

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

When this enters the JSP page, I can refer to the instance of myObject model as:

${myObject}

and the list inside

${myObject.myFoo}

What I want to want is to specify the size of myFoo on my JSP output, for example:

${myObject.myFoo.size}

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

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

+4
source share
2 answers

JSTL, JSP-. : https://code.google.com/p/dlcode/downloads/detail?name=jstl-quick-reference.pdf

taglib :

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

:

${fn:length(myObject.myFoo)}
+5

( ) getter:

public getMyFooSize() { return myFoo.size(); }

JSP:

${myObject.myFooSize}
+1

Source: https://habr.com/ru/post/1618034/


All Articles