JSTL tag Choose When Example Code

JSP/JSTL choose when tag example Program


In JSP we can perform string operations without using any java code inside scriptlets. This is possible with the latest release of JSTL 1.1. The new library called Functions with prefix as fn allows us to perform string operations in JSP 2.0.
This Choose When tag example explains all the string operation which can perform with the new fn tags. Note:
* You will need the following files to run this example :
* jstl.jar and standard.jar inside /WEB-INF/lib
* fn.tld, c.tld inside any location under any lcation under WEB-INF
Example
1. Create a file as sample.jsp in your webapps folder and add the below code

<%@ page contentType="text/html" %>
<html>
<body>
<form name="myForm" method="post" action="jstl-choose-tag.jsp">
Select an operation and click the button<br /><br />
<input type="radio" name="radioBtnGroup" value="uCaseOp" />Convert a string to upper Case<br />
<input type="radio" name="radioBtnGroup" value="lCaseOp" />Convert a string to lower Case<br />
<input type="radio" name="radioBtnGroup" value="subStrOp" />Get a substring of a string<br />
<input type="radio" name="radioBtnGroup" value="subStrAfterOp" />Get substring after<br />
<input type="radio" name="radioBtnGroup" value="subStrBeforeOp" />Get substring before<br />
<input type="radio" name="radioBtnGroup" value="trimOp" />Trim the string<br />
<input type="radio" name="radioBtnGroup" value="replaceOp" />Replaces characters in a string<br />
<input type="radio" name="radioBtnGroup" value="splitOp" />Split a string<br />
<input type="radio" name="radioBtnGroup" value="joinOp" />Join a string after it has been split<br />
<input type="radio" name="radioBtnGroup" value="escapeXml" />Escapes XML characters in a string<br />
<input type="radio" name="radioBtnGroup" value="indexOf" />Find start index of a substring in a string<br />
<input type="radio" name="radioBtnGroup" value="startsWithOp" />Check if the string starts with a particular substring<br />
<input type="radio" name="radioBtnGroup" value="endsWithOp" />Check if the string end with a particular substring<br />
<input type="radio" name="radioBtnGroup" value="containsOp" />Check if the string contains a particular substring(Case Sensitive)<br />
<input type="radio" name="radioBtnGroup" value="containsIgnoreCaseOp" />Check if the string contains a particular substring(Case In-sensitive)<br />
<input type="radio" name="radioBtnGroup" value="lengthOp" />Length of a String<br />
<br />
<input type="Submit" name="submitBtn" value="Click" />
</form>
</body>
</html>



2. Create a file as jstl-choose-tag.jsp

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<body>
<c:choose>
<c:when test="${param.radioBtnGroup=='uCaseOp'}">
Source String : california
<br />
UpperCase : ${fn:toUpperCase('california')}
</c:when>
<c:when test="${param.radioBtnGroup=='lCaseOp'}">
Source String : CALIFORNIA
<br />
Lowercase : ${fn:toLowerCase('CALIFORNIA')}
</c:when>
<c:when test="${param.radioBtnGroup=='subStrOp'}">
Source String : California
<br />
Substring : ${fn:substring('California',4,7)}
</c:when>
<c:when test="${param.radioBtnGroup=='subStrAfterOp'}">
Source String : California
<br />
Substring After "Cali" : ${fn:substringAfter('California','Cali')}
</c:when>
<c:when test="${param.radioBtnGroup=='subStrBeforeOp'}">
Source String : California
<br />
Substring Before "fornia" : ${fn:substringBefore('California','fornia')}
</c:when>
<c:when test="${param.radioBtnGroup=='trimOp'}">
Source String : California
<br />
Trim the string " California " : ${fn:trim(' California ')}
</c:when>
<c:when test="${param.radioBtnGroup=='replaceOp'}">
Source String : California
<br />
Replace : ${fn:replace('California','a','A')}
</c:when>
<c:when test="${param.radioBtnGroup=='splitOp'}">
Source String : Cali;fornia
<br />
Split Separator : ";"
<br />
After Split an array is created with two items:
<br />
Item at index 0 : ${fn:split('Cali;fornia ',';')[0]}
<br />
Item at index 1 : ${fn:split('Cali;fornia ',';')[1]}
</c:when>
<c:when test="${param.radioBtnGroup=='joinOp'}">
Source String : Cali;fornia
<br />
Split Separator : ";"
<br />
Join Separator : "|"
<br />
Join :
<c:set var="a1" value="${fn:split('Cali;fornia', ';')}" />
<c:out value="${fn:join(a1, '|')}" />
</c:when>
<c:when test="${param.radioBtnGroup=='escapeXml'}">
${fn:escapeXml('<city>California</City>')}
</c:when>
<c:when test="${param.radioBtnGroup=='indexOf'}">
Source String : California
<br />
Index Of "for" : ${fn:indexOf('California','for')}
<br />
</c:when>
<c:when test="${param.radioBtnGroup=='startsWithOp'}">
Source String : California
<br />
Starts With "Cal" : ${fn:startsWith('California','Cal')}
</c:when>
<c:when test="${param.radioBtnGroup=='endsWithOp'}">
Source String : California
<br />
Ends With "nia" : ${fn:endsWith('California','nia')}
</c:when>
<c:when test="${param.radioBtnGroup=='containsOp'}">
Source String : California
<br />
Contains "FOR" : ${fn:contains('California','FOR')}
</c:when>
<c:when test="${param.radioBtnGroup=='containsIgnoreCaseOp'}">
Source String : California
<br />
Contains IgnoreCase "FOR" : ${fn:containsIgnoreCase('California','FOR')}
</c:when>
<c:when test="${param.radioBtnGroup=='lengthOp'}">
Source String : California
<br />
Length of the string : ${fn:length('California')}
</c:when>
<c:otherwise>
Please make a selection to perform an operation
</c:otherwise>
</c:choose>

<br /><br />
<a href="sample.jsp"><< Back</a>
</body>
</html>



Related Topic How to Specify Content Type and Character Set (X)HTML Element
How to Create a New Project with Native Android View Controls
RSS Feed Reader Parser in JSP
JSP XML file parser
Difference between Abstraction and Encapsulation?
JSP  JSTL  

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback