この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:3
ページ更新者:guest
更新日時:2026-06-11 07:29:05

タイトル: テンプレートフラグメント(ヘッダー等の共有化)
SEOタイトル: テンプレートフラグメント(ヘッダー等の共有化)

この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<thead>
		<tr><th style="background-color:#f39c12;color:#ffffff;text-align:left">この記事の要点</th></tr>
	</thead>
	<tbody>
		<tr>
			<td style="background-color:#fef9e7">
			<ul>
				<li>Spring (Thymeleaf) の<strong>テンプレートフラグメント</strong>: ヘッダー / フッターの共通化</li>
				<li>定義: 共通部品ファイルに <code>th:fragment="name"</code> を付ける</li>
				<li>読み込み: <code>th:replace="~{path/fragment :: name}"</code> または <code>th:insert</code></li>
				<li>部分置換できるので、ページごとに差分だけ書ける</li>
			</ul>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>



<p>本稿は<strong>Spring Frameworkのテンプレートフラグメント</strong>について説明します。</p>

<p>&nbsp;</p>

<h2>テンプレートフラグメントとは?</h2>

<p>テンプレートフラグメントを利用することで、ヘッダーやフッター等を部品化し、共有化させることができます。</p>

<p>&nbsp;</p>

<h2>フラグメントの作成</h2>

<p>以下の内容の/src/main/resource/templates/test/fragment.htmlを作成します。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff"><!DOCTYPE html></span></p>

			<p><span style="color:#ffffff"><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"></span></p>

			<p style="margin-left:40px"><span style="color:#ffffff">&nbsp; <body></span></p>

			<p style="margin-left:80px"><span style="color:#ffffff">&nbsp; &nbsp; <!-- Header --></span></p>

			<p style="margin-left:80px"><span style="color:#ffffff">&nbsp; &nbsp; <div </span><span style="color:#f1c40f">th:fragment="header"</span><span style="color:#ffffff">></span></p>

			<p style="margin-left:120px"><span style="color:#ffffff">&nbsp; &nbsp; <p>ヘッダー</p></span></p>

			<p style="margin-left:80px"><span style="color:#ffffff">&nbsp; &nbsp; </div></span></p>

			<p style="margin-left:80px">&nbsp;</p>

			<p style="margin-left:80px"><span style="color:#ffffff">&nbsp; &nbsp; <!-- Footer --></span></p>

			<p style="margin-left:80px"><span style="color:#ffffff">&nbsp; &nbsp; <div </span><span style="color:#f1c40f">th:fragment="footer"</span><span style="color:#ffffff">></span></p>

			<p style="margin-left:120px"><span style="color:#ffffff">&nbsp; &nbsp; <p>フッター</p></span></p>

			<p style="margin-left:80px"><span style="color:#ffffff">&nbsp; &nbsp; </div></span></p>

			<p style="margin-left:40px"><span style="color:#ffffff">&nbsp; </body></span></p>

			<p><span style="color:#ffffff"></html></span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>サンプルページの作成</h2>

<p>フラグメントを取り込むサンプルページを作成します。</p>

<p>以下の内容の/src/main/resource/templates/test/content.htmlを実装します。</p>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff"><!DOCTYPE HTML></span></p>

			<p><span style="color:#ffffff"><html xmlns:th="http://www.thymeleaf.org"></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; <head></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; <title>Content Page</title></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; &nbsp; &nbsp; <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; </head></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; <body></span></p>

			<p style="margin-left:40px"><span style="color:#ffffff">&nbsp; &nbsp; <!-- Header --></span></p>

			<p style="margin-left:40px"><span style="color:#ffffff">&nbsp; &nbsp; <div </span><span style="color:#f1c40f">th:include="/test/fragment :: header"</span><span style="color:#ffffff">></div></span></p>

			<p style="margin-left:40px">&nbsp;</p>

			<p style="margin-left:40px"><span style="color:#ffffff">&nbsp; &nbsp; <!-- Content --></span></p>

			<p style="margin-left:40px"><span style="color:#ffffff">&nbsp; &nbsp; <p>コンテント</p></span></p>

			<p style="margin-left:40px">&nbsp;</p>

			<p style="margin-left:40px"><span style="color:#ffffff">&nbsp; &nbsp; <!-- Footer --></span></p>

			<p style="margin-left:40px"><span style="color:#ffffff">&nbsp; &nbsp; <div </span><span style="color:#f1c40f">th:include="/test/fragment :: footer"</span><span style="color:#ffffff">></div></span></p>

			<p><span style="color:#ffffff">&nbsp; &nbsp; </body></span></p>

			<p><span style="color:#ffffff"></html>&nbsp;</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>

<h2>出力結果</h2>

<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
	<tbody>
		<tr>
			<td style="background-color:#000000">
			<p><span style="color:#ffffff">ヘッダー</span></p>

			<p><span style="color:#ffffff">コンテント</span></p>

			<p><span style="color:#ffffff">フッター</span></p>
			</td>
		</tr>
	</tbody>
</table>

<p>&nbsp;</p>