JSP/Servlet | JSTL(JSP Standard Tag Library) | 基本関数 Functions

JSTLには開発の利便性のために、いくつかの基本関数が用意されている。ここではその基本関数について説明する。

ディレクティブ宣言

基本関数を使用するには、JSPの上部に次のディレクティブを宣言する必要がある。

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

関数

関数名 戻り値 使い方 説明
contains boolean fn:contains(A, B) 文字列Aに文字列Bが含まれているか確認する。
containsIgnoreCase boolean fn:containsIgnoreCase(A, B) 大文字小文字を区別せず、AにBが含まれているか確認する。
endWith boolean fn:endWith(A, B) 文字列Aの末尾がBで終わるか確認する。
escapeXml String fn:escapeXml(A) AにXMLやHTMLで定義された文字列が含まれる場合、エンティティコードへ変換する。
indexOf int fn:indexOf(A, B) 文字列AでBが最初に現れるindexを返す。
join String fn:join(A[], B) 文字列配列Aを区切り文字Bで結合し、1つの文字列に変換する。
length int fn:length(A) Collectionオブジェクト、ListやArrayListの全体の長さを返す。
replace String fn:replace(A, B, C) 文字列AでBに該当する文字を探し、Cへ変換する。
split String[] fn:split(A, B) AをBで指定した文字列で分割し、配列として返す。
startsWith String fn:startsWith(A, B) 文字列AがBで始まるか確認する。
substring String fn:substring(A, B, C) Aでindex番号BからCまでに該当する文字列を返す。
substringAfter String fn:substringAfter(A, B) AでBが現れた後の文字列を返す。
substringBefore String fn:substringBefore(A, B) AでBが現れる前の文字列を返す。
toLowerCase String fn:toLowerCase(A) Aをすべて小文字に変換する。
toUpperCase String fn:toUpperCase(A) Aをすべて大文字に変換する。
trim String fn:trim(A) 文字列Aの前後の空白を削除する。

使用例

toLowerCase, toUpperCase

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
   <head>
      <title>JSTL Functions</title>
   </head>
   <body>
      <c:set var="str" value="My name is DevKama"/>
      <p>Lower Case : ${fn:toLowerCase(str)}</p>
      <p>Upper Case : ${fn:toUpperCase(str)}</p>
   </body>
</html>

出力結果

my name is devkama
MY NAME IS DEVKUMA