[JAVA] Reintroduction to JSTL

Introduction

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 expression (Expression Language)

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.

NG example

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.

OK example

String data = "It is a test.";

pageContext.setAttribute("data", data);

<%= data %>  // OK

<c:out value=${data} /> // OK

Usable tag library

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.

core tag

Introduction

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

tag

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

Usage example

<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>

fmt tag

Introduction

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

tag

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

Usage example: Date format

<fmt:formatDate value="${data.updateDate}" pattern="yyyy/MM/dd HH:mm"/>

functions tag

Introduction

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

tag

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

Usage example

<c:set var="favs" value="Circer, reading" />

<c:out value="${fn:contains(favs, 'reading') ? 'readingが大好きです。' : 'readingに興味がありません。' }"></c:out>

sql

Introduction

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

Details: https://www.javatpoint.com/jstl-sql-tags

xml

Introduction

<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>

Details: https://www.javatpoint.com/jstl-xml-tags

that's all

Recommended Posts

Reintroduction to JSTL
to_ ○
Switch from JSP + JSTL to Thymeleaf
Reintroduction to Operators with RxJava Part 1
Association (1 to 1)! !!