As we discussed, Model 2 consists of Java Servlet, EL(Expression Language), and JSTL(JSP Standard Tag Library). In this post, we will learn about JSTL.
JSLT, itself stands for JSP Standard Tag Library. So what do we need to do first?
To download the library!
First, go to apache taglibs.
https://tomcat.apache.org/taglibs/standard/
And click the download.
That's actually it for the configuration!
These are the five types of tags in JSTL. Among then, we use mostly the core and Internationalization library.
Tags | Description | Example |
Core | Provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc. | <%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %> |
Internationalization | For formatting of Numbers, Dates and i18n support through locales and resource bundles. | <%@ taglib uri="https://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> |
SQL | Provide support for interaction with relational databases such as Oracle, MySql etc. Using JSTL SQL tags we can run database queries. | <%@ taglib uri="https://java.sun.com/jsp/jstl/sql" prefix="sql" %> |
XML |
Used to work with XML documents such as parsing XML, transforming XML data, and evaluating XPath expressions. | <%@ taglib uri="https://java.sun.com/jsp/jstl/xml" prefix="x" %> |
Functions |
Provide many functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc. | <%@ taglib uri="https://java.sun.com/jsp/jstl/functions" prefix="fn" %> |
Here are some most used JSTL Core Tags and examples of them.
Tags | Description |
c:set | Sets the result of an expression under evaluation in a 'scope' variable. |
c:if | Conditional tag used for testing the condition and display the body content only if the expression evaluates is true. |
c:choose | Simple conditional tag that includes its body content if the evaluated condition is true. |
c:forEach | Repeats the nested body content for fixed number of times or over collection. |
c:import | Retrieves relative or an absolute URL and display the contents to either a String in 'var',a Reader in 'varReader' or the page. |
c:out | Displays the result of an expression, similar to the way <%=...%> tag work. |
Set tag / Remove tag
<%@ page contentType = "text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="num1" value="${20}" />
<c:set var="num2">
10.5
</c:set>
<c:set var="today" value="<%= new java.util.Date() %>" />
<html>
<head>
<title>set Tag & remove Tag</title>
</head>
<body>
Variable num1 = ${num1} <br>
Variable num2 = ${num2} <br>
num1 + num2 = ${num1 + num2} <br>
Today is ${today}.
<c:remove var="num1" scope="page" />
<p>
num1 after deletion = ${num1} <br>
num1 + num2 after deletion = ${num1 + num2}
</body>
</html>
As you can see above, EL is used to print out in JSTL, which means you can use EL by itself, but JSTL has to collaborate with EL.
You need to use the request object to use the EL tag.
<%
String str = "JSP Variable";
request.setAttribute("st", str);
%>
Variable1: str1 = <%=str %> <br> <!-- Printed -->
Variable2: str2 = ${str} <br> <!-- Not printed -->
Variable3: str3 = ${st} <br> <!-- Printed -->
if tag
if tag is to process a single condition.
ifTagForm.jsp
<%@ page contentType = "text/html; charset=utf-8" %>
<html>
<body>
<form method=post action=ifTag.jsp>
Name <input type=text name=name><br>
Age <input type=text name=age><br>
<input type=submit value="Submit">
</form>
</body>
</html>
ifTag.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:if test="true">
If the test value is "true", it always runs. <br>
</c:if>
<c:if test="${param.name == 'Meadow' }">
Hello ${param.name }, welcome! <br>
</c:if>
<c:if test="${param.age >= 18 }">
You are an adult! haha!<br>
</c:if>
choose tag
choose ~ when ~ otherwise tag is used when we process multiple conditions.
It is the same as switch ~ case ~ default in java.
If there is a condition that the value meets, it will execute the tag and stop execution.
ifTagFrom.jsp
<%@ page contentType = "text/html; charset=utf-8" %>
<html>
<body>
<form method=post action=chooseTag.jsp>
Name <input type=text name=name><br>
Bloodtype <input type=text name=bloodtype><br>
<input type=submit value="Submit">
</form>
</body>
</html>
chooseTag.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<ul>
<c:choose>
<c:when test="${param.name == 'Meadow' }">
<li>Your name is ${param.name }.</li>
</c:when>
<c:when test="${param.bloodtype == 'O' }">
<li>Your bloodtype is O! People with type O blood less
susceptible to malaria.</li>
</c:when>
<c:when test="${param.bloodtype == 'A' }">
<li>Your bloodtype is A! People with type A blood are more
likely to develop stomach cancer.</li>
</c:when>
<c:when test="${param.bloodtype == 'AB' }">
<li>Your bloodtype is AB! People with type AB blood are much
more likely to develop cognitive issues.</li>
</c:when>
<c:when test="${param.bloodtype == 'B' }">
<li>Your bloodtype is B! About 9% of the population have B
positive blood.</li>
</c:when>
<c:otherwise>
<li>Your name is not Meadow, and there is no such bloodtype.
</li>
</c:otherwise>
</c:choose>
</ul>
</body>
</html
If I enter the name "Meadow", it will stop executing at the first condition since it meets the condition.
If I enter the other name and the blood type(O,A,AB, or B), it will execute that condition.
catch tag / out tag
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>JSTL core Library Example 1</title>
</head>
<body>
<c:set var="test" value="Hello JSTL!" />
<h3>
Using <c:set> :
<c:out value="${test}" />
</h3>
<c:remove var="test" />
<h3>
Using <c:remove> :
<c:out value="${test}" />
</h3>
<c:catch var="err">
<%=10 / 0%>
</c:catch>
<h3>
An error caught by <c:catch>:
<c:out value="${err}" />
</h3>
<c:if test="${5<10}">
<h3>5 is lesser than 10.</h3>
</c:if>
<c:if test="${6+3==9}">
<h3>6 + 3 equals 9.</h3>
</c:if>
<c:choose>
<c:when test="${5+10==50}">
<h3>5+10 equals 50.</h3>
</c:when>
<c:otherwise>
<h3>5+10 not equal 50.</h3>
</c:otherwise>
</c:choose>
</body>
</html>
forEach tag
It is like a loop in java, while the format is different from it.
There are two big types in this forEach tag. Type 1 is more used than the other. Let us look at the example codes.
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
java.util.HashMap mapData = new java.util.HashMap();
mapData.put("name", "Meadow");
mapData.put("today", new java.util.Date());
%>
<c:set var="intArray" value="<%=new int[] { 1, 2, 3, 4, 5 }%>" />
<c:set var="map" value="<%=mapData%>" />
<html>
<head>
<title>forEach Tag</title>
</head>
<body>
<h4>Sum of odd integers from 1 to 100.</h4>
<c:set var="sum" value="0" />
<c:forEach var="i" begin="1" end="100" step="2">
<c:set var="sum" value="${sum + i}" />
</c:forEach>
Result = ${sum}
<h4>4 Times table</h4>
<ul>
<c:forEach var="i" begin="1" end="9">
<li>4 * ${i} = ${4 * i}
</c:forEach>
</ul>
<h4>Array - Datatype int</h4>
<c:forEach var="i" items="${intArray}" begin="2" end="4">
[${i}]
</c:forEach>
<h4>Map</h4>
<c:forEach var="i" items="${map}">
${i.key} = ${i.value}<br>
</c:forEach>
<br>
<br>
<%
List list = new ArrayList();
list.add("Toronto");
list.add("Halifax");
list.add("Vancouver");
list.add("Calgary");
list.add("Airdrie");
list.add("Winnipeg");
list.add("Yellowknife");
request.setAttribute("slist", list);
%>
<!-- Type 1 -->
<c:forEach var="s" items="${slist}">
${s} <br>
</c:forEach>
<br>
<br>
<%
// Origianl code
List li = (List) request.getAttribute("slist");
for (int i = 0; i < li.size(); i++) {
String str = (String) li.get(i);
out.println(str + "<br>");
}
%><br><br>
<!-- Type 2 -->
<c:set var="s1" value="<%=list %>"/>
<c:forEach var="s2" items="${s1}">
${s2} <br>
</c:forEach>
</body>
</html>
Other tags(not used a lot)
forTokens tag
<%@ page contentType = "text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>forTokens tag</title></head>
<body>
delims= ",." :<br>
<c:forTokens var="token"
items="Red,Orange.Yellow.Green,Blue,Navy.Purple"
delims=",.">
${token}
</c:forTokens>
</body>
</html>
forEach tag / forTokens tag
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>JSTL core Library Example</title>
</head>
<body>
<c:forEach var="test" begin="1" end="10" step="2">
<b>${test}</b>
</c:forEach>
<br>
<c:forTokens var="alphabet" items="a,b,c,d,e,f,g,h,i,j,k,l,m,n"
delims=",">
<b>${alphabet}</b>
</c:forTokens>
<br>
<c:set var="data" value="Banana,Orange,Melon" />
<c:forTokens var="varData" items="${data}" delims=",">
<b>${varData}</b>
</c:forTokens>
</body>
</html>
So far, we have learned about Core tags, which are the most used, in the next post, we will see the second most used ones, internationalization tags.
'Java > JSP' 카테고리의 다른 글
JSP) JSTL - Function tags / SQL tags (0) | 2022.09.19 |
---|---|
JSP) JSTL - Internationalization tags (0) | 2022.09.19 |
JSP) EL(Expression Language) (0) | 2022.09.16 |
JSP) Model2 - Java Servlet 1 (0) | 2022.09.15 |
JSP) Model1 vs Model2 (0) | 2022.09.15 |