25
Jan

Unfortunately, there are no functions in EL concerning Strings. This may be of interest when you want to display different data than delivered from a backing bean, and provide the logic in the logic in the view (pro: can be easily modified, con: logic in the view?). There are several ways (client-side with JavaScript/server-side with Java), but I want to show how to use (already existing) JSTL functions inside EL Expression without writing any extra Java nor JavaScript code:

  • Add JSTL functions namespace in Facelets XHTML:

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

  • JSTL function startsWith inside EL expression

#{fn:startsWith(beanName.fieldName,'PREFIX_')}

  • JSF Example: filtering values starting with a prefix:

<h:outputText value="#{fn:startsWith(beanName.fieldName,'PREFIX_')} ? '' : beanName.fieldName "/>

The other JSTL functions are, see JSTL Functions API documentation: http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/tlddocs/fn/tld-summary.html

  • 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
  • length: Returns the number of items in a collection, or the number of characters in a string

, , ,

2 Responses to “String functions in JSF inside EL (JSF Expression Language)”

Add reply