質問するログイン新規登録

質問編集履歴

1

誤字の修正

2023/12/11 14:44

投稿

hiko_00001
hiko_00001

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,265 +1,265 @@
1
- ### 前提・実現したいこと
2
- 現在、JavanSpringBOOTを用いてお問い合わせアプリを作成しています。
3
-
4
- DBに登録済みの問い合わせデータを更新する際に空文字にしてDB登録をしようとした場合に
5
- バリデーションを表示させたいのですが、できないため、バリデーションの表示方法をご教示いただきたいです。
6
- ![イメージ説明](b4e81ca07e4810fd0e1aafb1d0401fa4.png)
7
- ※1送信ボタンを押下後、DBへの登録は変更されていないです。
8
- ※2上記写真は送信ボタンを押下後にバリデーションが表示されていないものです。
9
- ※2このアプリは下記URLから利用できます。
10
- http://inquirysystem-env.eba-2fdyge2m.ap-northeast-1.elasticbeanstalk.com/inquiry
11
- ※3問い合わせを登録する際にはバリデーションが表示されます。
12
- ![イメージ説明](7923d5a1c476bcc3c53b5e0c918b8a01.png)
13
-
14
- ### 発生している問題・エラーメッセージ
15
-
16
- ```
17
- 1枚目の写真の通りバリデーションが表示されません。
18
-
19
- ```
20
- ### 該当のソースコード
21
- InquiryController
22
- ```Java
23
- package com.example.demo.app.inquiry;
24
-
25
- import java.time.LocalDateTime;
26
- import java.util.List;
27
- import java.util.Optional;
28
- import javax.validation.Valid;
29
- import org.springframework.stereotype.Controller;
30
- import org.springframework.ui.Model;
31
- import org.springframework.validation.BindingResult;
32
- import org.springframework.validation.annotation.Validated;
33
- import org.springframework.web.bind.annotation.GetMapping;
34
- import org.springframework.web.bind.annotation.ModelAttribute;
35
- import org.springframework.web.bind.annotation.PathVariable;
36
- import org.springframework.web.bind.annotation.PostMapping;
37
- import org.springframework.web.bind.annotation.RequestMapping;
38
- import org.springframework.web.bind.annotation.RequestParam;
39
- import org.springframework.web.servlet.mvc.support.RedirectAttributes;
40
- import com.example.demo.entity.Inquiry;
41
- import com.example.demo.service.InquiryService;
42
-
43
- @Controller
44
- @RequestMapping("/inquiry")
45
- public class InquiryController {
46
-
47
- private final InquiryService inquiryService;
48
- public InquiryController(InquiryService inquiryService) {
49
-
50
- this.inquiryService=inquiryService;
51
- }
52
-      
53
- @GetMapping("/{id}")
54
- public String showUpdate(
55
- InquiryForm inquiryForm,
56
- @PathVariable int id,
57
- Model model) {
58
-
59
- //Inquiryを取得(Optionalでラップ)
60
- Optional<Inquiry> inquiryOpt = inquiryService.getInquiry(id);
61
-
62
- //InquiryFormへの詰め直し
63
- Optional<InquiryForm> inquiryFormOpt = inquiryOpt.map(t->makeInquiryForm(t));
64
- //InquiryFormがnullでなければ中身を取り出し
65
- if(inquiryFormOpt.isPresent()) {
66
- inquiryForm = inquiryFormOpt.get();
67
- }
68
-
69
- model.addAttribute("InquiryForm", inquiryForm);
70
- List<Inquiry> list = inquiryService.getAll();
71
- model.addAttribute("list", list);
72
- model.addAttribute("inquiryId", id);
73
- model.addAttribute("title", "更新用フォーム");
74
-
75
- return "inquiry/update";
76
- }
77
-
78
- @PostMapping("/update")
79
- public String update(
80
- @ModelAttribute @Validated InquiryForm inquiryForm,
81
- BindingResult result,
82
- @RequestParam("inquiryId") int inquiryId,
83
- Model model,
84
- RedirectAttributes redirectAttributes) {
85
-
86
- if (!result.hasErrors()) {
87
- //InquiryFormのデータをInquiryに格納
88
- Inquiry inquiry=new Inquiry();
89
- inquiry.setId(inquiryId);
90
- inquiry.setName(inquiryForm.getName());
91
- inquiry.setEmail(inquiryForm.getEmail());
92
- inquiry.setContents(inquiryForm.getContents());
93
- inquiry.setCreated(LocalDateTime.now());
94
-
95
- //更新処理、フラッシュスコープの使用、リダイレクト(個々の編集ページ)
96
- inquiryService.update(inquiry);
97
- redirectAttributes.addFlashAttribute("complete", "変更が完了しました");
98
- return "redirect:/inquiry/" + inquiryId;
99
-
100
- }else {
101
- model.addAttribute("InquiryForm", inquiryForm);
102
- model.addAttribute("inquiryId",inquiryId);
103
- model.addAttribute("title", "更新用フォーム");
104
- return "inquiry/update";
105
- }
106
- }
107
- @PostMapping("/delete")
108
- public String delete(
109
- @RequestParam("inquiryId") int inquiryid,
110
- Model model) {
111
- //タスクを一件削除しリダイレクト
112
- inquiryService.delete(inquiryid);
113
- return "redirect:/inquiry";
114
- }
115
- private InquiryForm makeInquiryForm(Inquiry inquiry) {
116
- InquiryForm InquiryForm = new InquiryForm();
117
- InquiryForm.setName(inquiry.getName());
118
- InquiryForm.setEmail(inquiry.getEmail());
119
- InquiryForm.setContents(inquiry.getContents());
120
- return InquiryForm;
121
- }
122
- }
123
-
124
- ```
125
- InquiryForm
126
- ```Java
127
- package com.example.demo.app.inquiry;
128
-
129
- import javax.validation.constraints.Email;
130
- import javax.validation.constraints.NotNull;
131
- import javax.validation.constraints.Size;
132
-
133
- public class InquiryForm {
134
- @NotNull(message="お名前を入力してください")
135
- @Size(min =1,max=20,message="20文字以下で入力してください")
136
- private String name;
137
-
138
- @NotNull(message="メールアドレスを入力してください")
139
- @Email(message="メールアドレスのフォーマットで入力してください")
140
- private String email;
141
- @NotNull(message="問い合わせ内容を入力してください")
142
- private String contents;
143
-
144
- public InquiryForm() {
145
-
146
- }
147
- //getterとsetterは省略
148
-
149
- }
150
- ```
151
- index_boot.html
152
- ```HTML
153
- <html xmlns:th="http://www.thymeleaf.org">
154
- <head>
155
- <link rel="icon" href="../../../../favicon.ico">
156
-
157
- <title>お問い合わせ一覧</title>
158
-
159
-
160
- </head>
161
- <body>
162
- <div class="starter-template">
163
- <h1 th:text="${title}">お問い合わせ一覧</h1>
164
- <p class="lead">お問い合わせ内容の一覧です。</p>
165
- </div>
166
-
167
- <table class="table table-striped">
168
- <thead>
169
- <tr>
170
- <th scope="col">ID</th>
171
- <th scope="col">名前</th>
172
- <th scope="col">メールアドレス</th>
173
- <th scope="col">問い合わせ内容</th>
174
- <th scope="col">日時</th>
175
- <th scope="col"></th>
176
- <th scope="col"></th>
177
- </tr>
178
- </thead>
179
- <tbody>
180
- <tr th:each="inquiry : ${inquiryList}">
181
- <td scope="row" th:text="${inquiry.id}">1</td>
182
- <td th:text="${inquiry.name}">name</td>
183
- <td th:text="${inquiry.email}">email</td>
184
- <td th:text="${inquiry.contents}">contents</td>
185
- <td th:text="${inquiry.created}">created</td>
186
- <td><a type="button" th:href="@{/inquiry/{id}(id=${inquiry.id})}">編集</a></td>
187
- <td>
188
- <form method="POST" th:action="@{/inquiry/delete}">
189
- <input type="hidden" name="inquiryId" th:value="${inquiry.id}">
190
- <input type="submit" value="削除" class="btn btn-primary">
191
- </form>
192
- </td>
193
- </tr>
194
-
195
- </tbody>
196
- </table>
197
-
198
-
199
- </main><!-- /.container -->
200
-
201
- </body>
202
- </html>
203
- ```
204
-
205
- update.html
206
- ```HTML
207
- <html xmlns:th="http://www.thymeleaf.org"
208
- xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
209
- <head>
210
- <meta charset="utf-8">
211
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
212
- <meta name="description" content="">
213
- <meta name="author" content="">
214
- <link rel="icon" href="../../../../favicon.ico">
215
- <title>更新用フォーム</title>
216
-
217
- <!-- Bootstrap core CSS -->
218
- <link href="/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
219
-
220
- <!-- Custom styles for this template -->
221
- <link href="starter-template.css" th:href="@{/css/starter-template.css}" rel="stylesheet">
222
- </head>
223
-
224
- <body>
225
- <div th:replace="~{block/header::headerA}"></div>
226
- <main role="main" class="container">
227
-
228
- <h1 th:text="${title}">更新用フォーム</h1>
229
- <p th:if="${complete}" th:text="${complete}"></p>
230
- <form method="post" action="#" th:action="@{/inquiry/update}" th:object="${InquiryForm}">
231
-
232
- <label for="name">お名前</label>
233
- <input type="text" name="name" class="form-control" id="name" th:value="*{name}">
234
- <div class="text-danger mb-4" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
235
-
236
- <label for="email">メールアドレス</label>
237
- <input type="text" name="email" class="form-control" id="email" th:value="*{email}">
238
- <div class="text-danger mb-4" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
239
-
240
- <label for="contents">問い合わせ内容</label>
241
- <textarea name="contents" class="form-control" id="detail" rows="3" th:field="*{contents}"></textarea>
242
- <div class="text-danger mb-4" th:if="${#fields.hasErrors('contents')}" th:errors="*{contents}"></div>
243
-
244
- <input th:if="${inquiryId}" type="hidden" name="inquiryId" th:value="${inquiryId}">
245
- <button type="submit" style="margin-top:2px" class="btn btn-primary">送信</button><br>
246
-
247
- <a href="#" style="margin-top:10px" th:href="@{/inquiry}" class="btn btn-primary">戻る</a>
248
- </form><br>
249
- </main>
250
- </form>
251
- </div>
252
- </body>
253
- </html>
254
- ```
255
- ### 試したこと
256
- update.htmlのソースコードに間違いがないか確認しましたがわかりませんでした。
257
-
258
- ### 補足情報(FW/ツールのバージョンなど)
259
- OS:macOS Big Sur 11.5.2
260
- 使用ブラウザ:GoogleChrome
261
- Spring:2.5.4
262
- Java:OpenJDK 11.0.2
263
- DB:MySQL(AWSのRDS上で起動中)
264
-
1
+ ### 前提・実現したいこと
2
+ 現在、JavaSpringBootを用いてお問い合わせアプリを作成しています。
3
+
4
+ DBに登録済みの問い合わせデータを更新する際に空文字にしてDB登録をしようとした場合に
5
+ バリデーションを表示させたいのですが、できないため、バリデーションの表示方法をご教示いただきたいです。
6
+ ![イメージ説明](b4e81ca07e4810fd0e1aafb1d0401fa4.png)
7
+ ※1送信ボタンを押下後、DBへの登録は変更されていないです。
8
+ ※2上記写真は送信ボタンを押下後にバリデーションが表示されていないものです。
9
+ ※2このアプリは下記URLから利用できます。
10
+ http://inquirysystem-env.eba-2fdyge2m.ap-northeast-1.elasticbeanstalk.com/inquiry
11
+ ※3問い合わせを登録する際にはバリデーションが表示されます。
12
+ ![イメージ説明](7923d5a1c476bcc3c53b5e0c918b8a01.png)
13
+
14
+ ### 発生している問題・エラーメッセージ
15
+
16
+ ```
17
+ 1枚目の写真の通りバリデーションが表示されません。
18
+
19
+ ```
20
+ ### 該当のソースコード
21
+ InquiryController
22
+ ```Java
23
+ package com.example.demo.app.inquiry;
24
+
25
+ import java.time.LocalDateTime;
26
+ import java.util.List;
27
+ import java.util.Optional;
28
+ import javax.validation.Valid;
29
+ import org.springframework.stereotype.Controller;
30
+ import org.springframework.ui.Model;
31
+ import org.springframework.validation.BindingResult;
32
+ import org.springframework.validation.annotation.Validated;
33
+ import org.springframework.web.bind.annotation.GetMapping;
34
+ import org.springframework.web.bind.annotation.ModelAttribute;
35
+ import org.springframework.web.bind.annotation.PathVariable;
36
+ import org.springframework.web.bind.annotation.PostMapping;
37
+ import org.springframework.web.bind.annotation.RequestMapping;
38
+ import org.springframework.web.bind.annotation.RequestParam;
39
+ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
40
+ import com.example.demo.entity.Inquiry;
41
+ import com.example.demo.service.InquiryService;
42
+
43
+ @Controller
44
+ @RequestMapping("/inquiry")
45
+ public class InquiryController {
46
+
47
+ private final InquiryService inquiryService;
48
+ public InquiryController(InquiryService inquiryService) {
49
+
50
+ this.inquiryService=inquiryService;
51
+ }
52
+      
53
+ @GetMapping("/{id}")
54
+ public String showUpdate(
55
+ InquiryForm inquiryForm,
56
+ @PathVariable int id,
57
+ Model model) {
58
+
59
+ //Inquiryを取得(Optionalでラップ)
60
+ Optional<Inquiry> inquiryOpt = inquiryService.getInquiry(id);
61
+
62
+ //InquiryFormへの詰め直し
63
+ Optional<InquiryForm> inquiryFormOpt = inquiryOpt.map(t->makeInquiryForm(t));
64
+ //InquiryFormがnullでなければ中身を取り出し
65
+ if(inquiryFormOpt.isPresent()) {
66
+ inquiryForm = inquiryFormOpt.get();
67
+ }
68
+
69
+ model.addAttribute("InquiryForm", inquiryForm);
70
+ List<Inquiry> list = inquiryService.getAll();
71
+ model.addAttribute("list", list);
72
+ model.addAttribute("inquiryId", id);
73
+ model.addAttribute("title", "更新用フォーム");
74
+
75
+ return "inquiry/update";
76
+ }
77
+
78
+ @PostMapping("/update")
79
+ public String update(
80
+ @ModelAttribute @Validated InquiryForm inquiryForm,
81
+ BindingResult result,
82
+ @RequestParam("inquiryId") int inquiryId,
83
+ Model model,
84
+ RedirectAttributes redirectAttributes) {
85
+
86
+ if (!result.hasErrors()) {
87
+ //InquiryFormのデータをInquiryに格納
88
+ Inquiry inquiry=new Inquiry();
89
+ inquiry.setId(inquiryId);
90
+ inquiry.setName(inquiryForm.getName());
91
+ inquiry.setEmail(inquiryForm.getEmail());
92
+ inquiry.setContents(inquiryForm.getContents());
93
+ inquiry.setCreated(LocalDateTime.now());
94
+
95
+ //更新処理、フラッシュスコープの使用、リダイレクト(個々の編集ページ)
96
+ inquiryService.update(inquiry);
97
+ redirectAttributes.addFlashAttribute("complete", "変更が完了しました");
98
+ return "redirect:/inquiry/" + inquiryId;
99
+
100
+ }else {
101
+ model.addAttribute("InquiryForm", inquiryForm);
102
+ model.addAttribute("inquiryId",inquiryId);
103
+ model.addAttribute("title", "更新用フォーム");
104
+ return "inquiry/update";
105
+ }
106
+ }
107
+ @PostMapping("/delete")
108
+ public String delete(
109
+ @RequestParam("inquiryId") int inquiryid,
110
+ Model model) {
111
+ //タスクを一件削除しリダイレクト
112
+ inquiryService.delete(inquiryid);
113
+ return "redirect:/inquiry";
114
+ }
115
+ private InquiryForm makeInquiryForm(Inquiry inquiry) {
116
+ InquiryForm InquiryForm = new InquiryForm();
117
+ InquiryForm.setName(inquiry.getName());
118
+ InquiryForm.setEmail(inquiry.getEmail());
119
+ InquiryForm.setContents(inquiry.getContents());
120
+ return InquiryForm;
121
+ }
122
+ }
123
+
124
+ ```
125
+ InquiryForm
126
+ ```Java
127
+ package com.example.demo.app.inquiry;
128
+
129
+ import javax.validation.constraints.Email;
130
+ import javax.validation.constraints.NotNull;
131
+ import javax.validation.constraints.Size;
132
+
133
+ public class InquiryForm {
134
+ @NotNull(message="お名前を入力してください")
135
+ @Size(min =1,max=20,message="20文字以下で入力してください")
136
+ private String name;
137
+
138
+ @NotNull(message="メールアドレスを入力してください")
139
+ @Email(message="メールアドレスのフォーマットで入力してください")
140
+ private String email;
141
+ @NotNull(message="問い合わせ内容を入力してください")
142
+ private String contents;
143
+
144
+ public InquiryForm() {
145
+
146
+ }
147
+ //getterとsetterは省略
148
+
149
+ }
150
+ ```
151
+ index_boot.html
152
+ ```HTML
153
+ <html xmlns:th="http://www.thymeleaf.org">
154
+ <head>
155
+ <link rel="icon" href="../../../../favicon.ico">
156
+
157
+ <title>お問い合わせ一覧</title>
158
+
159
+
160
+ </head>
161
+ <body>
162
+ <div class="starter-template">
163
+ <h1 th:text="${title}">お問い合わせ一覧</h1>
164
+ <p class="lead">お問い合わせ内容の一覧です。</p>
165
+ </div>
166
+
167
+ <table class="table table-striped">
168
+ <thead>
169
+ <tr>
170
+ <th scope="col">ID</th>
171
+ <th scope="col">名前</th>
172
+ <th scope="col">メールアドレス</th>
173
+ <th scope="col">問い合わせ内容</th>
174
+ <th scope="col">日時</th>
175
+ <th scope="col"></th>
176
+ <th scope="col"></th>
177
+ </tr>
178
+ </thead>
179
+ <tbody>
180
+ <tr th:each="inquiry : ${inquiryList}">
181
+ <td scope="row" th:text="${inquiry.id}">1</td>
182
+ <td th:text="${inquiry.name}">name</td>
183
+ <td th:text="${inquiry.email}">email</td>
184
+ <td th:text="${inquiry.contents}">contents</td>
185
+ <td th:text="${inquiry.created}">created</td>
186
+ <td><a type="button" th:href="@{/inquiry/{id}(id=${inquiry.id})}">編集</a></td>
187
+ <td>
188
+ <form method="POST" th:action="@{/inquiry/delete}">
189
+ <input type="hidden" name="inquiryId" th:value="${inquiry.id}">
190
+ <input type="submit" value="削除" class="btn btn-primary">
191
+ </form>
192
+ </td>
193
+ </tr>
194
+
195
+ </tbody>
196
+ </table>
197
+
198
+
199
+ </main><!-- /.container -->
200
+
201
+ </body>
202
+ </html>
203
+ ```
204
+
205
+ update.html
206
+ ```HTML
207
+ <html xmlns:th="http://www.thymeleaf.org"
208
+ xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
209
+ <head>
210
+ <meta charset="utf-8">
211
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
212
+ <meta name="description" content="">
213
+ <meta name="author" content="">
214
+ <link rel="icon" href="../../../../favicon.ico">
215
+ <title>更新用フォーム</title>
216
+
217
+ <!-- Bootstrap core CSS -->
218
+ <link href="/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
219
+
220
+ <!-- Custom styles for this template -->
221
+ <link href="starter-template.css" th:href="@{/css/starter-template.css}" rel="stylesheet">
222
+ </head>
223
+
224
+ <body>
225
+ <div th:replace="~{block/header::headerA}"></div>
226
+ <main role="main" class="container">
227
+
228
+ <h1 th:text="${title}">更新用フォーム</h1>
229
+ <p th:if="${complete}" th:text="${complete}"></p>
230
+ <form method="post" action="#" th:action="@{/inquiry/update}" th:object="${InquiryForm}">
231
+
232
+ <label for="name">お名前</label>
233
+ <input type="text" name="name" class="form-control" id="name" th:value="*{name}">
234
+ <div class="text-danger mb-4" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
235
+
236
+ <label for="email">メールアドレス</label>
237
+ <input type="text" name="email" class="form-control" id="email" th:value="*{email}">
238
+ <div class="text-danger mb-4" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
239
+
240
+ <label for="contents">問い合わせ内容</label>
241
+ <textarea name="contents" class="form-control" id="detail" rows="3" th:field="*{contents}"></textarea>
242
+ <div class="text-danger mb-4" th:if="${#fields.hasErrors('contents')}" th:errors="*{contents}"></div>
243
+
244
+ <input th:if="${inquiryId}" type="hidden" name="inquiryId" th:value="${inquiryId}">
245
+ <button type="submit" style="margin-top:2px" class="btn btn-primary">送信</button><br>
246
+
247
+ <a href="#" style="margin-top:10px" th:href="@{/inquiry}" class="btn btn-primary">戻る</a>
248
+ </form><br>
249
+ </main>
250
+ </form>
251
+ </div>
252
+ </body>
253
+ </html>
254
+ ```
255
+ ### 試したこと
256
+ update.htmlのソースコードに間違いがないか確認しましたがわかりませんでした。
257
+
258
+ ### 補足情報(FW/ツールのバージョンなど)
259
+ OS:macOS Big Sur 11.5.2
260
+ 使用ブラウザ:GoogleChrome
261
+ Spring:2.5.4
262
+ Java:OpenJDK 11.0.2
263
+ DB:MySQL(AWSのRDS上で起動中)
264
+
265
265
  足りない情報等ありましたらコメントくださると幸いです。