Jstl Fortokens Tag Event - Split Upward String Inward Jsp
JSTL forTokens tag is to a greater extent than or less other tag inward pith JSTL library to back upwards Iteration or looping. It effectively complements, to a greater extent than useful <c:forEach> tag, past times allowing you lot to iterate over comma separated or whatever delimited String. You tin purpose this tag to separate string inward JSP as well as tin operate on them individually. forTokens tag has similar attribute similar forEach JSTL tag except 1 to a greater extent than attribute called delims, which specifies delimiter. For instance to iterate over colon separated String "abc:cde:fgh:ijk", delims=":". By the way, forTokens tag likewise bring multiple delimiter, which means, you lot tin split a big string into token based upon multiple delimiter e.g. colon(:) as well as pipe (|), This volition endure to a greater extent than clear, when nosotros volition run across examples of JSTL forTokens tag inward JSP. Rest of attribute e.g. items, var, varStatus, begin, end as well as step are same, equally they are inward instance of <c:forEach> tag. For quick review, items specify String which needs to endure split-ed inward token as well as var concord electrical flow String.
JSTL <c:forTokens> Tag Example
Here is our consummate code instance of using JSTL forTokens tag inward JSP page. In this example, nosotros outset separate a comma separate String using forTokens tag past times specifying delims=";". When nosotros iterate over tokens, var correspond electrical flow token. In the minute example, nosotros produce got specified multiple delimiter inward delims attribute, delims="|," to separate String past times pipe(|) grapheme as well as comma (,) character. <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> JSTL forTokens tag Example - Iterate over comma separated String inward JSP</title>
</head>
<body>
<h3> forTokens Tag Example</h3>
<h4>String amongst comma ',' delimiter</h4>
<c:forTokens var="token" items="Java, J2EE, JSTL, JSP, Servlet"
delims=",">
<c:out value="${token}"/> </br>
</c:forTokens>
<h4>Iterating over String amongst '|' as well as ',' delimiters</h4>
<c:forTokens var="token" items="USA,New York,UK|London,Japan|Tokyo"
delims="|,">
<c:out value="${token}"/> </br>
</c:forTokens>
</body>
</html>
Output:
forTokens Tag Example
String amongst comma ',' delimiter
Java
J2EE
JSTL
JSP
Servlet
Iterating over String amongst '|' as well as ',' delimiters
USA
New York
UK
London
Japan
Tokyo
That's all on How to purpose JSTL forTokens tag for iterating over comma separated String. Good matter close forTokens tag is that, it non exclusively complements forEach tag, merely likewise back upwards multiple delimiters for breaking String into tokens. Quite handy to procedure asking parameters as well as other text data.
Further Learning
Spring Framework 5: Beginner to Guru
Java Web Fundamentals By Kevin Jones
JSP, Servlets as well as JDBC for Beginners: Build a Database App
Komentar
Posting Komentar