- JSPでは標準的なタグ以外によく利用する機能を実装したカスタムタグを応用することが可能である。カスタムタグの作成方法はこの授業の範囲を超えるため解説はしない。しかしながら、一般的にはカスタムタグ機能を利用して、第三者が作成したカスタムタグがタグライブラリという形で使われることが多い。そこで、ここではよく使われるタグライブラリとして、JSTL(JSP Standard Tag Library)を取り上げ、その簡単な使い方について解説を行う
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
- JARファイル(jstl-1.2.jar)をウェブアプリケーションのクラスパス(WEB-INF/lib)に入れる
- JSPページにtaglibディレクティブでJSTLタグライブラリの利用を以下のような形で宣言する
- prefixはタグを使う場合の接頭辞であり、どのような文字列を使ってもよいが、一般的にJSTLの基本(core)ライブラリを使う場合には"c"とする
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="[変数名]">[変数に代入する値]</c:set>
1 2 3 4 5 6 7 8
|
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<c:set var="str">セットされた文字列</c:set>
変数strの値は「<b>${str}</b>」です。
</body>
</html> |
<c:if test="[条件式:EL式でtrueかfalseを返すもの]">
条件式がtrueだった場合に実行される処理
</c:if>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<form action="${pageContext.request.requestURI}">
あなたの年齢は?<input type="text" name="age" value="${param.age}">
</form>
<c:if test="${param.age < 20}">
あなたは未成年ですね。
</c:if>
<c:if test="${param.age >= 20}">
あなたは立派な大人ですね。
</c:if>
</body>
</html> |
- c:chooseタグはc:ifタグと異なり複数の条件を扱う場合に用いる
- c:chooseタグの書式
<c:choose>
<c:when test="[条件式1]">
条件式1が成り立った場合に行う処理
</c:when>
<c:when test="[条件式2]">
条件式2が成り立った場合に行う処理
</c:when>
</c:choose>
- c:redirectタグはページを遷移させたい場合に用いる
- c:redirectタグの書式
<c:redirect url="[遷移させたいURL]" >
パラメータを設定したい場合には・・・
</c:redirect>
- jsp:forwardとc:redirectの違い
- c:chooseタグ、c:redirectタグの例:jstl_chooseAndRedirect.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
宛先を決めてください。
<form action="${pageContext.request.requestURI}">
<input type="radio" name="redirect" value="1" />Google
<input type="radio" name="redirect" value="2" />Yahoo!
<input type="submit" value="行先へ飛ぶ" />
</form>
<c:choose>
<c:when test="${param.redirect eq 1}">
<c:redirect url="http://www.google.com/" />
</c:when>
<c:when test="${param.redirect eq 2}">
<c:redirect url="http://www.yahoo.co.jp/" />
</c:when>
</c:choose>
</body>
</html> |
- 繰り返し処理を記述するのに用いるタグ
- 書式1:繰り返し
<c:forEach begin="[開始値]" end="[終了値]" var="[繰り返し数が格納される変数名]">
繰り返される処理
</c:forEach>
- c:forEachタグの例1:jstl_forEach1.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
<%@ page pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<!-- JSTLで書いた例 -->
<c:forEach begin="1" end="7" var="i">
<font size="${i}">
font-size:${i}<br />
</font>
</c:forEach>
<!-- スクリプレットで書いた例 -->
<% for(int i=1; i<=7; i++) {%>
<font size=<%=i%>>
font-size:<%=i%><br />
</font>
<% } %>
</body>
</html> |
- 書式2:コレクション(集合)に基づく繰り返し(参照)
<c:forEach items="[コレクション]" var="[参照するときの変数名]">
繰り返し処理
</c:forEach>
- c:forEachタグの例2:jstl_forEach2.jsp
1 2 3 4 5 6 7 8 9 10 11
|
<%@ page pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<c:set var="firstFoods"
value="マクドナルド,モスバーガー,ロッテリア,サブウェイ,ウェンディーズ,Beckers,フレッシュネスバーガー,小僧寿し?" />
<c:forEach items="${firstFoods}" var="firstFood">
${firstFood}<br />
</c:forEach>
</body>
</html> |
- 曜日を調べるJSP:jstl_calendar1.jsp
- selectタグで年月日を入力させるのにforeachタグを用いている
- selectタグ中のoptionで入力された文字列と同じ場合には三項演算子によって選択された状態を設定している
- 年月日が入力されているかをifタグを用いて分岐している
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Locale"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>曜日を調べるページ1</title>
</head>
<body>
<form action="${pageContext.request.requestURI}">
<select name="year">
<c:forEach begin="1970" end="2012" var="year">
<option value="${year}" ${(param.year eq year)?'selected':''}>${year}</option>
</c:forEach>
</select>
年
<select name="month">
<c:forEach begin="1" end="12" var="month">
<option value="${month}" ${(param.month eq month)?'selected':''}>${month}</option>
</c:forEach>
</select>
月
<select name="date">
<c:forEach begin="1" end="31" var="date">
<option value="${date}" ${(param.date eq date)?'selected':''}>${date}</option>
</c:forEach>
</select>
日
<input type="submit" value="曜日を調べる">
</form>
<c:if test="${!empty param.year}">
<%
Calendar calendar = Calendar.getInstance();
pageContext.setAttribute("calendar",calendar);
calendar.set(Integer.parseInt(request.getParameter("year")),Integer.parseInt(request.getParameter("month"))-1,Integer.parseInt(request.getParameter("date")));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日はEEE曜日です",Locale.JAPANESE);
pageContext.setAttribute("answer",dateFormat.format(calendar.getTime()));
%>
${answer}
</c:if>
</body>
</html> |
- 曜日を調べるJSP:jstl_calendar2.jsp
- 上記の例をさらにfmtタグを使ってスクリレットを用いずに書いた例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>曜日を調べるページ</title>
</head>
<body>
<form action="${pageContext.request.requestURI}">
<select name="year">
<c:forEach begin="1970" end="2012" var="year">
<option value="${year}" ${(param.year eq year)?'selected':''}>${year}</option>
</c:forEach>
</select>
年
<select name="month">
<c:forEach begin="1" end="12" var="month">
<option value="${month}" ${(param.month eq month)?'selected':''}>${month}</option>
</c:forEach>
</select>
月
<select name="date">
<c:forEach begin="1" end="31" var="date">
<option value="${date}" ${(param.date eq date)?'selected':''}>${date}</option>
</c:forEach>
</select>
日
<input type="submit" value="曜日を調べる">
</form>
<c:if test="${!empty param.year}">
<!-- Date型に変換:パラメータを受け取ってDate型に変換(パース)する -->
<fmt:parseDate var="date" value="${param.year}/${param.month}/${param.date}" />
<!-- ロケールの設定:ここでは日本に設定する -->
<fmt:setLocale value="ja_JP"/>
<!-- フォーマット出力:出力したい形式にフォーマットして出力 -->
<fmt:formatDate value="${date}" pattern="yyyy年MM月dd日はEEE曜日です" />
</c:if>
</body>
</html> |
- 上記のjstl_if.jspを参考にし、年齢を尋ね、中学生未満か中学生か中学生よりも年上かを答えるプログラムを作成しなさい(08-01if.jsp)。
- 上記のjstl_chooseAndRedirect.jspを改造し、gooも選択肢に入れるような修正を行いなさい(08-02choose.jsp)。
- 上記のjstl_calendar.jspを改造し、土曜日であれば青色、日曜日であれば、赤色で表示されるように修正しなさい(/kadai/08-03calendar.jsp)。
最終更新時間:2018年11月29日 09時28分05秒