JSP標準タグライブラリ(JSTL: JavaServer Pages Standard Tag Library)とは、JSPバージョン2.0で標準化されたタグの集まりである。

バージョン

JSTLのバージョン一覧と、それぞれのJSTLに対応するJSPとServletのバージョンを示す。

バージョン 対応するJSP 対応するServlet EL式
JSTL 1.2 JSP 2.1 Servlet 2.5 使用できる
JSTL 1.1 JSP 2.0 Servlet 2.4 使用できる
JSTL 1.0 JSP 1.2 Servlet 2.3 使用できない

ダウンロード

JSTLはMaven Repositoryからダウンロードできる。

JavaServer Pages Standard Tag Library

タグ

JSTLのタグはCore、XML、Internationalization、Database及びFunctionsの5つに大別される。

JSTLのタグ
Area Prefix URI
Core c http://java.sun.com/jsp/jstl/core
XML x http://java.sun.com/jsp/jstl/xml
Internationalization fmt http://java.sun.com/jsp/jstl/fmt
Database sql http://java.sun.com/jsp/jstl/sql
Functions fn http://java.sun.com/jsp/jstl/functions

JSTLを使うにはtaglib宣言を行う必要がある。prefixには任意の名前を指定できるが、慣習的に下記の名前を使うことが多い。

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

古いJSTLバージョン1.0の場合は、URIが異なる。

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jstl/functions" prefix="fn" %>

JSTL Coreタグ

CoreはJSTLの核となる基本的なタグの集まりである。

JSTLのcoreタグ
タグ 説明
<c:catch> 例外を補足する。
<c:out> 値を出力する。
<c:set> 変数に値を設定する。
<c:forEach> 繰り返し処理
<c:forTokens> 文字列を区切り文字で分けたトークンを順次処理する。
<c:if> 条件分岐
<c:choose> switch case分岐
<c:when>
<c:otherwise>
<c:import> 外部のコンテンツをインポートする。
<c:param> <c:import><c:url><c:redirect>で使用するパラメータを設定する。
<c:redirect> 指定したURLへリダイレクトさせる。
<c:remove> 変数を削除する。
<c:url> URLをエンコードする。

JSTL Internationalizationタグ

Internationalization (i18n)は、国際化に関するJSTLタグの集まりである。

JSTL i18nタグ
タグ 説明
<fmt:formatDate> 日付データを指定した書式で出力する。
<fmt:formatNumber> 数値データを指定した書式で出力する。
<fmt:message> リソースのメッセージを取得する。
<fmt:parseNumber> 文字列を数値データに変換する。
<fmt:parseDate> 文字列を日付データに変換する。
<fmt:requestEncoding> リクエストの文字エンコーディングを設定する。
<fmt:setLocale> ロケールを設定する。
<fmt:setTimeZone> タイムゾーンを設定する。
<fmt:timeZone> タイムゾーンを設定する。

JSTL Functions

FunctionsはJSTL関数の集まりである。

JSTL Functions
Function Description
fn:contains(string, substring) 文字列の中に部分文字列が含まれているかどうかを返す。
fn:containsIgnoreCase(string, substring) 文字列の中に部分文字列が含まれているかどうかを返す。比較の際、大文字と小文字は区別しない。
fn:endsWith(string, suffix) 指定した文字列で終わるかどうかを返す。
fn:escapeXml(string) 文字列に含まれるXML予約文字をエスケープする。
fn:indexOf(string, substring) 部分文字列が出現する位置を返す。
fn:join(array, separator) 配列の各要素を連結して文字列として返す。
fn:length(object) コレクションの項目数又は文字列の文字数を返す。
fn:replace(string, before, after) 文字列を置き換える。
fn:split(string, separator) 文字列を分割して配列で返す。
fn:startsWith(string, prefix) 指定した文字列で始まるかどうかを返す。
fn:substring(string, start, end) 部分文字列を返す。
fn:substringAfter(string, substring) 指定文字列以降の文字列を返す。
fn:substringBefore(string, substring) 指定文字列以前の文字列を返す。