Wednesday 21 October 2015

Get the size of a collection in EL using JSTL

Although the java.util.Collection interface defines a size method, it does not conform to the JavaBeans component design pattern for properties and so cannot be accessed by using the JSP expression language.

The JSTL length function can be applied to any collection supported by the c:forEach and returns the length of the collection.

When applied to a String, it returns the number of characters in the string.

Add the JSTL functions namespace to the page/fragment:

xmlns:fn="http://java.sun.com/jsp/jstl/functions"

then you can use the length expression:

rendered="#{fn:length(bindings.actuatorList.allRowsInRange) > 0}">

Other useful JSTL functions:

  • toUpperCase, toLowerCase: Changes the capitalization of a string 
  • substring, substringBefore, substringAfter: Gets a subset of a string 
  • trim: Trims white space from a string 
  • replace: Replaces characters in a string 
  • indexOf, startsWith, endsWith, contains, containsIgnoreCase: Checks whether a string contains another string 
  • split: Splits a string into an array 
  • join: Joins a collection into a string 
  • escapeXml: Escapes XML characters in a string

No comments:

Post a Comment