Thursday 18 February 2016

JSTL functions and Core Tags- JSTL( , , Core Tags and >)

JSTL <c:set> Core Tag

<c:set> core JSTL tag is used for assigning a value to an object or variable within a specified scope

Attributes of <c:set> tag
1) value: It can be a hardcoded value or an expression. for e.g. below are the allowed variations of <c:set> tag –
The value of the variable myvar would be stored in the object name.
<c:set var="name" scope="application" value="${myvar}"/>
The result of the expression would be stored in the object.
<c:set var="sum" scope="application" value="${1+3+6}"/>
 
2) var: It holds the variable/object name
3) scope: It can be request, session, page and application. In the above example we have specified the scope as application, however it can be anything out of the mentioned four. It all depends on the requirements.

 

JSTL <c:if> Core Tag

 

<c:if> is a JSTL core tag which is used for testing conditions. It is more or like a if statement in java which evaluates a condition and executes a block of code if the result is true.

Example:-

Example of <c:if> tag

In this example we have defined age variable using <c:set> tag and then we are checking the eligibility of voting by using <c:if> tag.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL c:if Tag Example</title>
</head>
<body>
<c:set var="age" value="26"/>
<c:if test="${age >= 18}">
 <c:out value="You are eligible for voting!"/>
</c:if>
<c:if test="${age < 18}">
 <c:out value="You are not eligible for voting!"/>
</c:if>
</body>
</html>

 

JSTL <c:choose>, <c:when>, <c:otherwise> Core Tags

. These tags are used together like switch-case and default statements in java. <c:choose> is the one which acts like switch, <c:when> like case which can be used multiple times inside <c:choose> for evaluating different-2 conditions. <c:otherwise> is similar to default statement which works when all the <c:when> statements holds false.

<c:choose>
    <c:when test="${condition1}">
       //do something if condition1 is true
    </c:when>
    <c:when test="${condition2}">
        //do something if condition2 is true
    </c:when>
    <c:otherwise>
        //Statements which gets executed when all <c:when> tests are false.
    </c:otherwise>
</c:choose>
 
 
 Example:-
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:choose, c:when and c:otherwise Tag Example</title>
</head>
<body>
<c:set var="number1" value="${222}"/>
<c:set var="number2" value="${12}"/>
<c:set var="number3" value="${10}"/>
<c:choose>
 <c:when test="${number1 < number2}">
     ${"number1 is less than number2"}
 </c:when>
 <c:when test="${number1 <= number3}">
     ${"number1 is less than equal to number2"}
 </c:when>
 <c:otherwise>
     ${"number1 is largest number!"}
 </c:otherwise>
</c:choose>
</body>
</html>

 

JSTL <c:forEach> and <c:forTokens> Core Tags

<c:forEach> tag in JSTL is used for executing the same set of statements for a finite number of times. It’s similar to the for loop in java. This is basically used when we need to perform(execute) set of statements again and again for a specified number of times.

Syntax of <c:forEach>

<c:forEach var="counter_variable_name" begin="intial_value" end="final_limit">
    //Block of statements
</c:forEach>
 
The below are the three main attributes of <c:forEach> tag.
 
begin: The initial counter value.

end: The final limit till which the loop will execute

var: Counter variable name 

Example

In this example we are printing value of variable counter in loop using <c:forEach> tag. The loop is starting from value 1 (mentioned in begin attribute) and ending at value 10 (value of end attribute).
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Example c:forEach tag in JSTL</title>
</head>
<body>
<c:forEach var="counter" begin="1" end="10">
 <c:out value="${counter}"/>
</c:forEach>
</body>
</html>
c-forEach-example 
 
 

 

fn:indexOf() – JSTL Function

fn:indexOf() – JSTL Function

fn:indexOf() function is used for finding out the start position (index) of a string in the provided string.

Syntax

int indexOf(String,  String )
 
Points to Note:
  • The function returns -1 when the string is not found in the input string.
  • Function is case sensitive. It treats uppercase and lowercase character of same alphabet as different.
  • It returns the index of first occurrence which means if the string is present in input more than once than the function would return the index of first occurrence. Refer example.
 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>JSTL fn:indexOf() example</title>
</head>
<body>
${fn:indexOf("My name is Chaitanya Singh", "chaitanya")}
${fn:indexOf("My name is Chaitanya Singh", "Chaitanya")}
${fn:indexOf("This is an example", "is")}
${fn:indexOf("JSTL function - indexOf function", "function")}
</body>
</html>

fn-indexOf-example
 
 
 

 

fn:length() – JSTL Function

The JSTL function fn:length() is used for computing the length of a string or to find out the number of elements in a collection.

Syntax

int length(Object)
Return type of this function is int. It returns the length of the object provided in it as an argument.

Example of fn:length()

Here we are computing the length of the three different strings using the function.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>JSTL fn:length() example</title>
</head>
<body>
<c:set var="string1" value="This is String1"/>
<c:set var="string2" value="Hi"/>
<c:set var="string3" value="string3"/>
Length of String1 is: ${fn:length(string1)}<br>
Length of String2 is: ${fn:length(string2)}<br>
Length of String3 is: ${fn:length(string3)}
</body>
</html>
Example:-fn-length-example
 
 

 

 

 

 

No comments:

Post a Comment