0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

📘 Vol.8.3:Struts2で作る共通レイアウトJSP 〜header/footerを使った再利用設計〜

Last updated at Posted at 2025-05-21

本記事は、Struts2でJSPを書く際に頻出するタグの基本を網羅する
📘「Vol.8.0〜8.7:Struts2 タグライブラリ入門編(基本操作・最低限の知識)」 シリーズの一部です。

✅ 今回の目的

Struts2 + JSP開発において、共通のヘッダー・フッターを外部ファイルとして分離し、
<s:include> タグを活用して効率よくページ共通レイアウトを構築する方法を解説します。


🔹 共通ヘッダー/フッターの意義

  • レイアウト統一:すべての画面で同じ外観を保てる
  • メンテナンス性UP:ヘッダーの修正が1ヶ所で済む
  • 再利用性UP:ログインユーザー表示・リンク・共通CSSの適用なども一括管理可能

📁 共通ファイルの作成

header.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title><s:property value="pageTitle" default="掲示板システム" /></title>
    <link rel="stylesheet" href="<s:url value='/css/style.css' />" />
</head>
<body>
    <header>
        <h1>掲示板システム</h1>
        <p>
            ようこそ、<s:property value="#session.loggedInUser.user_name" default="ゲスト" /> さん |
            <a href="<s:url value='logout' />">ログアウト</a> |
            <a href="<s:url value='goToManagementMenu' />">管理メニュー</a>
        </p>
        <hr />
    </header>

footer.jsp

    <hr />
    <footer>
        <p>© 2025 BulletinBoard System. All rights reserved.</p>
    </footer>
</body>
</html>

📄 各画面での読み込み方法

<s:include value="/view/common/header.jsp" />

<!-- ここに個別の画面コンテンツを記述 -->

<s:include value="/view/common/footer.jsp" />

💡 応用ポイント

要素 内容
pageTitle ヘッダーで <title> を個別に変えたいときに使用
#session.loggedInUser.user_name セッションのユーザー名を表示
<s:url> アクションベースのリンク生成(メニュー遷移、ログアウト等)
共通CSS style.css を一括読み込みして全体デザイン統一

🔚 まとめ

共通のheader.jspfooter.jspを導入することで、ページレイアウトの統一・コードの再利用・メンテ性の向上が実現できます。
Struts2の <s:include> タグはその第一歩として非常に有効です。

📌 次回予定: 📘 Vol.8.4:【Struts2】「s:action」「s:token」タグ徹底解説!〜アクション呼び出しと二重送信防止の最前線〜 では <s:action><s:token> を使ったセキュアなフォーム実装について解説予定です!


📚 関連記事(Vol.8シリーズ)


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?