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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Java

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

Spring

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

Thymeleaf

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

Spring Boot

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

Q&A

解決済

2回答

6814閲覧

Thymeleafテンプレートが表示されない。【Spring Boot】

pepe0730

総合スコア8

Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Java

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

Spring

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

Thymeleaf

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

Spring Boot

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

0グッド

0クリップ

投稿2021/02/18 16:17

##状況

layout:fragmentを用いてhtmlテンプレート機能を実装しようとしていました。

しかし、テンプレート部分のhtmlが画面に表示されず困っています。

画面

##ソースコード

layout.html(テンプレート部分)

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <title>layout</title> </head> <body> <div class="container"> <h1 class="text-center text-primary mt-5">顧客管理システム</h1> <div> </div> <div layout:fragment="content"> Fake Content </div> </div> </body> </html>

list.html(コンテンツ部分)

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org/" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorator="~{layout}"> <head> <title>顧客一覧</title> </head> <body> <div layout:fragment="content" class="col-sm-12"> <form th:action="@{/customers/create}" th:object="${customerForm}" method="POST"> <dl> <dt><label for="lastName">姓</label></dt> <dd> <input type="text" id="lastName" name="lastName" th:field="*{lastName}" th:errorclass="error-input" value="山田" /> <span th:if="${#fields.hasErrors('lastName')}" th:errors="*{lastName}" class="error-messages">error!</span> </dd> </dl> <dl> <dt><label for="firstName">名</label></dt> <dd> <input type="text" id="firstName" name="firstName" th:field="*{firstName}" th:errorclass="error-input" value="太郎" /> <span th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}" class="error-messages">error!</span> </dd> </dl> <input type="submit" value="作成"> </form> <table> <tr th:each="customer : ${customers}"> <td th:text="${customer.id}">100</td> <td th:text="${customer.lastName}">山田</td> <td th:text="${customer.firstName}">太郎</td> <td> <form th:action="@{/customers/edit}" method="GET"> <input type="submit" name="form" value="編集"> <input type="hidden" name="id" th:value="${customer.id}" /> </form> </td> <td> <form th:action="@{/customers/delete}" method="POST"> <input type="submit" value="削除"> <input type="hidden" name="id" th:value="${customer.id}" /> </form> </td> </tr> </table> </div> </body> </html>

Contoroller

Java

1package com.example.thymeleaf_test.web; 2 3import java.util.List; 4import org.springframework.beans.BeanUtils; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Controller; 7import org.springframework.ui.Model; 8import org.springframework.validation.BindingResult; 9import org.springframework.validation.annotation.Validated; 10import org.springframework.web.bind.annotation.GetMapping; 11import org.springframework.web.bind.annotation.ModelAttribute; 12import org.springframework.web.bind.annotation.PostMapping; 13import org.springframework.web.bind.annotation.RequestMapping; 14import org.springframework.web.bind.annotation.RequestParam; 15 16import com.example.thymeleaf_test.domain.Customer; 17import com.example.thymeleaf_test.service.CustomerService; 18 19@Controller 20@RequestMapping("customers") 21public class CustomerController { 22 @Autowired 23 CustomerService customerService; 24 25 @ModelAttribute 26 CustomerForm steUpForm() { 27 return new CustomerForm(); 28 } 29 30 @GetMapping 31 String list(Model model) { 32 List<Customer> customers = customerService.findAll(); 33 model.addAttribute("customers", customers); 34 return "customers/list"; 35 } 36 37 @PostMapping(path = "create") 38 String create(@Validated CustomerForm form, BindingResult result, Model model) { 39 if (result.hasErrors()) { 40 return list(model); 41 } 42 Customer customer = new Customer(); 43 BeanUtils.copyProperties(form, customer); 44 customerService.create(customer); 45 return "redirect:/customers"; 46 } 47 48 @GetMapping(path = "edit", params = "form") 49 String editForm(@RequestParam Integer id, CustomerForm form) { 50 Customer customer = customerService.findOne(id); 51 BeanUtils.copyProperties(customer, form); 52 System.out.println(form); 53 return "customers/edit"; 54 } 55 56 @PostMapping(path = "edit") 57 String edit(@RequestParam Integer id, @Validated CustomerForm form, BindingResult result) { 58 if (result.hasErrors()) { 59 return editForm(id, form); 60 } 61 Customer customer = new Customer(); 62 BeanUtils.copyProperties(form, customer); 63 customerService.update(customer); 64 return "redirect:/customers"; 65 } 66 67 @PostMapping(path = "edit", params = "goToTop") 68 String goToTop() { 69 return "redirect:/customers"; 70 } 71 72 @PostMapping(path = "delete") 73 String delete(@RequestParam Integer id) { 74 customerService.delete(id); 75 return "redirect:/customers"; 76 } 77 78 79}

layout.htmlにあるh1やbootstrapが反映されるにはどうすれば良いのでしょうか。
エラーメッセージが出ない状況で原因が特定できないのでアドバイスお願いしたいです。

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

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

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

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

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

guest

回答2

0

自己解決

mavenにdocorateを追加したら解決しました。

投稿2021/02/20 01:23

pepe0730

総合スコア8

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

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

0

階層の指定が出来ていないのでは?
layout.htmlがどこに配置されているのかわからないので、
仮にlist.htmlと同階層にあるなら customers/layout と指定する必要があります。

投稿2021/02/19 07:29

Luice

総合スコア771

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

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

pepe0730

2021/02/19 07:31

layout.htmlはcustomersフォルダと同じ階層にあります。 layout.html customers ->list.html という風になっています。
Luice

2021/02/19 07:51

あと思いつくのはバージョンによる記述方法の違いですかね https://qiita.com/t-iguchi/items/7d52141fd0ddda0657e6 一度ミニマムなプロジェクトを作って検証か、動くサンプルと比較して どこに差分があるのかを地道に検証していくしかなさそうです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問