It is convenient to use JSTL when writing drawings with JSP, which has a long history. JSTL is the Java Server Pages Standard Tag Library.
If you use <% xxx%>
, you cannot organize the JSP format etc. in a hierarchical manner, so maintainability is poor.
For complex JSPs, split the JSP file and use the JSTL core tag to make branching, LOOP, etc. easier to understand.
EL expressions are described using the $ {~} tags.
Simple calculation example: $ {100 + 200 * 30}
, $ {"1 "eq" 1 "?" True ":" False "}
pageContext, request Example: $ {user.userName}
, $ {count}
Session example: $ {sessionScope.user.userName}
Initial parameter example: $ {initParam ["param1 "]}
Cookie example: $ {cookie ["param1 "] .value}
Header example: $ {header ['user-agent']}
How to write frequently used comparisons
1, equal: $ {a eq b}
2, not equal: $ {a ne b}
3、Not:${not a}
4、Empty:${empty a}
5、Not Empty:${not empty a}
6, greater than: $ {a gt b}
7, less than: $ {a lt b}
8, or more: $ {a ge b}
9, below: $ {a le b}
When writing in EL expression, it is necessary to set it.
String data = "It is a test.";
<%= data %> // OK
<c:out value=${data} /> //NG. data variable is request,Because it is not set in pageContext etc.
String data = "It is a test.";
pageContext.setAttribute("data", data);
<%= data %> // OK
<c:out value=${data} /> // OK
type | Description |
---|---|
core | forEach, if, variable reference/Basic tags such as settings |
fmt | Number and date formats, etc. |
xml | XML document parsing, XSL stylesheet |
functions | String general functions (contains, etc.) |
sql | Functions that issue SQL and operate databases |
Frequently used are core, function, fmt.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
tag | Description |
---|---|
c:set | Set variables |
c:remove | Delete variable |
c:out | output |
c:if | if statement (single conditional branch) |
c:choose, c:when, c:otherwise | if-else,if-elseif-else statement (multiple conditional branches) |
c:forEach | loop |
c:forTokens | Split a string with a delimiter |
c:import | Import file |
c:redirect | redirect |
c:url | Generate URL |
c:catch | Exception handling |
c:param | Specify parameters |
<c:set var="score" value="80" />
<c:choose>
<c:when test=${score gt 90}>
<c:out value="very impressive." />
</c:when>
<c:when test=${score gt 80}>
<c:out value="It is excellent." />
</c:when>
<c:otherwise>
<c:out value="Please do your best." />
</c:otherwise>
</c:choose>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
tag | Description |
---|---|
fmt:formatNumber | Format numeric data |
fmt:formatDate | Format date data |
fmt:parseNumber | Convert character strings to numeric data |
fmt:parseDate | Convert string to date data |
fmt:bundle, fmt:message | Get message for resource |
fmt:requestEncording> | Set the character encoding of the request |
fmt:setLocale | Set locale |
fmt:setTimeZone | Set time zone |
fmt:timeZone | Set time zone |
<fmt:formatDate value="${data.updateDate}" pattern="yyyy/MM/dd HH:mm"/>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
tag | Description |
---|---|
fn:contains() | Does it contain the specified character string? |
fn:containsIgnoreCase() | Does it contain the specified character string (case insensitive) |
fn:endsWith() | Is the last character string a specified character string? |
fn:escapeXml() | escape |
fn:indexOf() | Returns the index of the specified string. If it does not exist-1 |
fn:trim() | Remove whitespace before and after the string |
fn:startsWith() | Is the initial character string a specified character string? |
fn:split() | Divide by specified character string |
fn:toLowerCase() | Convert to lowercase |
fn:toUpperCase() | Convert to uppercase |
fn:substring() | Extract the character string within the specified range |
fn:substringAfter() | Extract after the specified character string |
fn:substringBefore() | Extract before the specified character string |
fn:length() | Returns the length of the string |
fn:replace() | Replacement |
<c:set var="favs" value="Circer, reading" />
<c:out value="${fn:contains(favs, 'reading') ? 'readingが大好きです。' : 'readingに興味がありません。' }"></c:out>
sql
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
Details: https://www.javatpoint.com/jstl-sql-tags
xml
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
Details: https://www.javatpoint.com/jstl-xml-tags
that's all