String manipulation functions inside EL

About

This article explains how to do string manipulation using JSTL functions inside EL Expression without writing any extra java/javascript code.

Introduction

JSTL functions tag library encapsulates some of the common operations performed on String like join, substring etc . These functions can be straight away used inside JSF EL expressions without much hassle.

Let's take substring function to explain how it can be used inside EL with an usecase of displaying partial description of substring function in an outputText component and the complete description in popup on clicking on More... (Do not get confused with the truncateAt property of the outputText, which can also be used to achieve this usecase. This usecase is just for explaining how to use JSTL functions with DF Faces).

Thumbnail image for SUBSTRING_RT_POPUP.JPG


To make the example simpler, we just use the bean property to store the function description


public class DescrBean {
	private String substringDescr = "This function finds and returns a subset of the string between the start index and the end index ";
	/**
	* @param substringDescr
	*/
	public void setSubstringDescr(String substringDescr) {
		this.substringDescr = substringDescr;
	}

	/**
	* @return substringDescr
	*/
	public String getSubstringDescr() {
		return substringDescr;
	}
}

Before proceeding to use the function tags in faces page, we should do following two things:

- Add JSTL Functions Tag Library to the project

ADD_JSTL_FUNCTIONS_TAG_LIB.JPG

- Add JSTL functions namespace

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

JSF Page Markup

The value of the outputText component is bound to the JSTL substring function which takes value of the bean property substringDescr as first parameter and displays only the first 14 characters of it.

<af:outputText id="ot1" value="#{fn:substring(backingBeanScope.DescrBean.substringDescr,0,13)}..."/>

A popup component is added to the same page with an outputText component bound to the same bean property used in substring function so

that the full string can be displayed in the popup

<!-- popup, to display complete description -->
<af:popup id="moreDesc">
	<af:dialog id="dlg1" closeIconVisible="false" type="ok" title="substring( java.lang.String, int, int)">
		<af:outputText id="ot2" value="#{backingBeanScope.DescrBean.substringDescr}"/>
	</af:dialog>
</af:popup>

Finally, the popup component is mapped to a 'more...' link so that the popup with full details will be displayed on clicking on 'more...' link

<af:commandLink id="cl1" text="More" partialSubmit="true">
	<af:showPopupBehavior popupId="moreDesc"/>
</af:commandLink>

In the same manner, other String functions like contains, containsIgnoreCase, endsWith, escapeXml, indexOf, join, length, replace, split, startsWith, substring, substringAfter, substringBefore, toLowerCase, toUpperCase and trim can also be used.

Reference

JSTL Functions API documentation.

Comments:

Cool...nice post!

Posted by Jobinesh on May 24, 2011 at 11:13 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tips and Tricks from Oracle's JDeveloper & ADF QA

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today