質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

4741閲覧

Springのthymeleafでテンプレートとコンテンツが同時に表示されない

G-ym

総合スコア16

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2018/11/14 15:17

編集2018/11/15 04:10

前提・実現したいこと

SpringBootでWebアプリケーションを作成しています。
thymeleafを使ってテンプレートとコンテンツを分けるような画面を作成しています。
しかし、コンテンツ部分を表示させるとテンプレート部分が消えてしまいます。
常にテンプレートを表示させる方法を教えてください。

発生している問題・エラーメッセージ

assetRegist.htmlを表示したとき
homeLayout.htmlの部分が表示されません。
home.htmlを表示したときはhomeLayout.htmlが表示されました。

該当のソースコード

homeLayout.html

html

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"> 3 4<head> 5 <meta charset="UTF-8"> 6 <link th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" rel="stylesheet"> 7 <script th:src="@{/webjars/jquery/1.11.1/jquery.min.js}"></script> 8 <script th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script> 9 <link th:href="@{/css/home.css}" rel="stylesheet"> 10 <title>Home</title> 11</head> 12 13<body> 14 <!--ナビゲーションバー--> 15 <nav class="navbar navbar-inverse navbar-fixed-top"> 16 <div class="container-fluid"> 17 <div class="navbar-header"> 18 <a class="navbar-brand" href="#">MonthlyCost</a> 19 </div> 20 <form method="post" th:action="@{/logout}"> 21 <button class="btn btn-link pull-right navbar-brand" type="submit">ログアウト</button> 22 </form> 23 </div> 24 </nav> 25 <!--サイドバー--> 26 <div class="container-fluid"> 27 <div class="row"> 28 <div class="col-sm-2 sidebar"> 29 <ul class="nav nav-pills nav-stacked"> 30 <li role="presentation"><a th:href="@{'/home'}">ホーム</a> 31 </li> 32 <li role="presentation"><a th:href="@{'/assetRegist'}">資産登録</a> 33 </li> 34 35 </ul> 36 </div> 37 </div> 38 </div> 39 <!--コンテンツ--> 40 <div class="container-fluid"> 41 <div class="row"> 42 <div class="col-sm-10 col-sm-offset-2 main"> 43 <div th:include="__${contents}__"></div> 44 </div> 45 </div> 46 </div> 47</body> 48 49</html> 50

assetRegist.html

HTML

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> 3 4<head> 5 <meta charset="UTF-8"> 6</head> 7 8<body> 9 <div th:fragment="assetRegist_contents"> 10 <div class="page-header"> 11 <h1>資産登録</h1> 12 </div> 13 <form method="post" th:action="@{/assetRegist}" th:object="${assetRegistrationForm}"> 14 <table class="table table-bordered table-hover"> 15 <tr> 16 <th class="active col-sm-3">名前</th> 17 <td> 18 <div class="form-group"> 19 <input type="text" class="form-control" th:field="*{assetName}"> 20 </div> 21 </td> 22 </tr> 23 <tr> 24 <th class="active">購入月</th> 25 <td> 26 <input type="text" class="form-control" placeholder="yyyy/MM/dd" th:field="*{purchaseDate}"> 27 </td> 28 </tr> 29 <tr> 30 <th class="active">予定使用期間(月)</th> 31 <td> 32 <input type="text" class="form-control" th:field="*{usedTerm}"> 33 </td> 34 </tr> 35 </table> 36 <button class="btn btn-primary" type="submit">資産登録</button> 37 </form> 38 </div> 39</body> 40 41</html> 42

home.html

HTML

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> 3 4<head> 5 <meta charset="UTF-8"> 6</head> 7 8<body> 9 <div th:fragment="home_contents"> 10 <div class="page-header"> 11 <h1>ホーム</h1> 12 </div> 13 </div> 14</body> 15 16</html> 17

java

1import org.springframework.beans.factory.annotation.Autowired; 2import org.springframework.dao.DataAccessException; 3import org.springframework.stereotype.Controller; 4import org.springframework.ui.Model; 5import org.springframework.validation.BindingResult; 6import org.springframework.validation.annotation.Validated; 7import org.springframework.web.bind.annotation.GetMapping; 8import org.springframework.web.bind.annotation.ModelAttribute; 9import org.springframework.web.bind.annotation.PostMapping; 10 11import com.example.demo.asset.domain.model.Asset; 12import com.example.demo.asset.domain.model.AssetRegistrationForm; 13import com.example.demo.asset.domain.service.AssetService; 14 15@Controller 16public class AssetRegistrationController { 17 18 @Autowired 19 AssetService assetService; 20 21 @GetMapping("/assetRegist") 22 public String getAssetRegistration(@ModelAttribute AssetRegistrationForm form,Model model) { 23 model.addAttribute("contents", "home/assetRegist :: assetRegist_contents"); 24 return "home/assetRegist"; 25 } 26 27 @PostMapping("/assetRegist") 28 public String postAssetRegistration(@ModelAttribute @Validated AssetRegistrationForm form,BindingResult bindResult,Model model) { 29 model.addAttribute("contents", "home/assetRegist :: assetRegist_contents"); 30// if (bindResult.hasErrors()) return getAssetRegistration(form,model); 31 System.out.println(form); 32 Asset asset = new Asset(); 33 asset.setAssetName(form.getAssetName()); 34 asset.setPurchaseDate(form.getPurchaseDate()); 35 asset.setUsedTerm(form.getUsedTerm()); 36 asset.setUserId("yamada@xxx.co.jp"); 37 try { 38 boolean result=assetService.insert(asset); 39 if (result) { 40 System.out.println("inset成功"); 41 }else { 42 System.out.println("inset失敗"); 43 } 44 } catch (DataAccessException e) { 45 System.out.println("inset失敗(エラー)"); 46 } 47 return "home/assetRegist"; 48 } 49 50} 51

java

1import java.util.List; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.web.bind.annotation.GetMapping; 7import org.springframework.web.bind.annotation.PostMapping; 8 9import com.example.demo.login.domain.model.User; 10import com.example.demo.login.domain.service.UserService; 11 12@Controller 13public class HomeController { 14 @Autowired 15 UserService userService; 16 17 @GetMapping("/home") 18 public String getHome(Model model) { 19 model.addAttribute("contents","home/home :: home_contents"); 20 return "home/homeLayout"; 21 } 22 @PostMapping("/logout") 23 public String posthome() { 24 return"redirect:/login"; 25 } 26 27 @GetMapping("/userList") 28 public String getUserList(Model model) { 29 model.addAttribute("contents","home/userList :: userList_contents"); 30 List<User> userList = userService.selectMany(); 31 model.addAttribute("userList",userList); 32 return "home/userList"; 33 } 34} 35

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

A-pZ

2018/11/15 02:41

「home.htmlを表示したときはhomeLayout.htmlが表示されました」とのことですが、Controllerにその記述はありませんでした。ソースコードに誤りや記載もれはないでしょうか。
G-ym

2018/11/15 04:11

申し訳ございません、HomeController の部分を質問に追記いたしました
guest

回答1

0

ベストアンサー

postAssetRegistrationメソッドは、return "home/assetRegist" としているので、assertRegist.html を表示します。

おそらく質問されている内容とファイルの関係が逆になっていて、
postAssetRegistrationメソッドを実行した後に return する値は home/homeLayout であり、
この homeLayhot.htmlが、th:includeで指定しているテンプレートのインクルード先:assertRegistを指定するのだと思います。

投稿2018/11/15 05:18

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

G-ym

2018/11/15 08:58

ありがとう御座います、解決できました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問