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?
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)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With