質問編集履歴

1

誤字の修正

2023/12/11 14:44

投稿

hiko_00001
hiko_00001

スコア7

test CHANGED
File without changes
test CHANGED
@@ -1,529 +1,265 @@
1
1
  ### 前提・実現したいこと
2
-
3
- 現在、JavanのSpringBOOTを用いてお問い合わせアプリを作成しています。
2
+ 現在、JavaのSpringBootを用いてお問い合わせアプリを作成しています。
4
-
5
-
6
3
 
7
4
  DBに登録済みの問い合わせデータを更新する際に空文字にしてDB登録をしようとした場合に
8
-
9
5
  バリデーションを表示させたいのですが、できないため、バリデーションの表示方法をご教示いただきたいです。
10
-
11
6
  ![イメージ説明](b4e81ca07e4810fd0e1aafb1d0401fa4.png)
12
-
13
7
  ※1送信ボタンを押下後、DBへの登録は変更されていないです。
14
-
15
8
  ※2上記写真は送信ボタンを押下後にバリデーションが表示されていないものです。
16
-
17
9
  ※2このアプリは下記URLから利用できます。
18
-
19
10
  http://inquirysystem-env.eba-2fdyge2m.ap-northeast-1.elasticbeanstalk.com/inquiry
20
-
21
11
  ※3問い合わせを登録する際にはバリデーションが表示されます。
22
-
23
12
  ![イメージ説明](7923d5a1c476bcc3c53b5e0c918b8a01.png)
24
13
 
25
-
26
-
27
14
  ### 発生している問題・エラーメッセージ
28
15
 
29
-
30
-
31
- ```
16
+ ```
32
-
33
17
  1枚目の写真の通りバリデーションが表示されません。
34
18
 
35
-
36
-
37
- ```
19
+ ```
38
-
39
20
  ### 該当のソースコード
40
-
41
21
  InquiryController
42
-
43
22
  ```Java
44
-
45
23
  package com.example.demo.app.inquiry;
46
24
 
47
-
48
-
49
25
  import java.time.LocalDateTime;
50
-
51
26
  import java.util.List;
52
-
53
27
  import java.util.Optional;
54
-
55
28
  import javax.validation.Valid;
56
-
57
29
  import org.springframework.stereotype.Controller;
58
-
59
30
  import org.springframework.ui.Model;
60
-
61
31
  import org.springframework.validation.BindingResult;
62
-
63
32
  import org.springframework.validation.annotation.Validated;
64
-
65
33
  import org.springframework.web.bind.annotation.GetMapping;
66
-
67
34
  import org.springframework.web.bind.annotation.ModelAttribute;
68
-
69
35
  import org.springframework.web.bind.annotation.PathVariable;
70
-
71
36
  import org.springframework.web.bind.annotation.PostMapping;
72
-
73
37
  import org.springframework.web.bind.annotation.RequestMapping;
74
-
75
38
  import org.springframework.web.bind.annotation.RequestParam;
76
-
77
39
  import org.springframework.web.servlet.mvc.support.RedirectAttributes;
78
-
79
40
  import com.example.demo.entity.Inquiry;
80
-
81
41
  import com.example.demo.service.InquiryService;
82
42
 
83
-
84
-
85
43
  @Controller
86
-
87
44
  @RequestMapping("/inquiry")
88
-
89
45
  public class InquiryController {
90
-
91
-
92
-
46
+
93
47
  private final InquiryService inquiryService;
94
-
95
48
  public InquiryController(InquiryService inquiryService) {
96
-
97
49
 
98
-
99
50
  this.inquiryService=inquiryService;
100
-
101
51
  }
102
-
103
52
       
104
-
105
53
  @GetMapping("/{id}")
106
-
107
54
  public String showUpdate(
108
-
109
55
  InquiryForm inquiryForm,
110
-
111
56
  @PathVariable int id,
112
-
113
57
  Model model) {
114
58
 
115
-
116
-
117
59
  //Inquiryを取得(Optionalでラップ)
118
-
119
60
  Optional<Inquiry> inquiryOpt = inquiryService.getInquiry(id);
120
61
 
121
-
122
-
123
62
  //InquiryFormへの詰め直し
124
-
125
63
  Optional<InquiryForm> inquiryFormOpt = inquiryOpt.map(t->makeInquiryForm(t));
126
-
127
64
  //InquiryFormがnullでなければ中身を取り出し
128
-
129
65
  if(inquiryFormOpt.isPresent()) {
130
-
131
66
  inquiryForm = inquiryFormOpt.get();
132
-
133
67
  }
134
68
 
135
-
136
-
137
69
  model.addAttribute("InquiryForm", inquiryForm);
138
-
139
70
  List<Inquiry> list = inquiryService.getAll();
140
-
141
71
  model.addAttribute("list", list);
142
-
143
72
  model.addAttribute("inquiryId", id);
144
-
145
73
  model.addAttribute("title", "更新用フォーム");
146
74
 
147
-
148
-
149
75
  return "inquiry/update";
150
-
151
76
  }
152
-
153
-
154
-
77
+
155
78
  @PostMapping("/update")
156
-
157
79
  public String update(
158
-
159
80
  @ModelAttribute @Validated InquiryForm inquiryForm,
160
-
161
81
  BindingResult result,
162
-
163
82
  @RequestParam("inquiryId") int inquiryId,
164
-
165
83
  Model model,
166
-
167
84
  RedirectAttributes redirectAttributes) {
168
85
 
169
-
170
-
171
86
  if (!result.hasErrors()) {
172
-
173
87
  //InquiryFormのデータをInquiryに格納
174
-
175
88
  Inquiry inquiry=new Inquiry();
176
-
177
89
  inquiry.setId(inquiryId);
178
-
179
90
  inquiry.setName(inquiryForm.getName());
180
-
181
91
  inquiry.setEmail(inquiryForm.getEmail());
182
-
183
92
  inquiry.setContents(inquiryForm.getContents());
184
-
185
93
  inquiry.setCreated(LocalDateTime.now());
186
-
187
94
 
188
-
189
95
  //更新処理、フラッシュスコープの使用、リダイレクト(個々の編集ページ)
190
-
191
96
  inquiryService.update(inquiry);
192
-
193
97
  redirectAttributes.addFlashAttribute("complete", "変更が完了しました");
194
-
195
98
  return "redirect:/inquiry/" + inquiryId;
196
-
197
99
 
198
-
199
100
  }else {
200
-
201
101
  model.addAttribute("InquiryForm", inquiryForm);
202
-
203
102
  model.addAttribute("inquiryId",inquiryId);
204
-
205
103
  model.addAttribute("title", "更新用フォーム");
206
-
207
104
  return "inquiry/update";
208
-
209
105
  }
210
-
211
106
  }
212
-
213
107
  @PostMapping("/delete")
214
-
215
108
  public String delete(
216
-
217
109
  @RequestParam("inquiryId") int inquiryid,
218
-
219
110
  Model model) {
220
-
221
111
  //タスクを一件削除しリダイレクト
222
-
223
112
  inquiryService.delete(inquiryid);
224
-
225
113
  return "redirect:/inquiry";
226
-
227
114
  }
228
-
229
115
  private InquiryForm makeInquiryForm(Inquiry inquiry) {
230
-
231
116
  InquiryForm InquiryForm = new InquiryForm();
232
-
233
117
  InquiryForm.setName(inquiry.getName());
234
-
235
118
  InquiryForm.setEmail(inquiry.getEmail());
236
-
237
119
  InquiryForm.setContents(inquiry.getContents());
238
-
239
120
  return InquiryForm;
240
-
241
121
  }
242
-
243
122
  }
244
123
 
245
-
246
-
247
- ```
124
+ ```
248
-
249
125
  InquiryForm
250
-
251
126
  ```Java
252
-
253
127
  package com.example.demo.app.inquiry;
254
128
 
255
-
256
-
257
129
  import javax.validation.constraints.Email;
258
-
259
130
  import javax.validation.constraints.NotNull;
260
-
261
131
  import javax.validation.constraints.Size;
262
132
 
263
-
264
-
265
133
  public class InquiryForm {
266
-
267
134
  @NotNull(message="お名前を入力してください")
268
-
269
135
  @Size(min =1,max=20,message="20文字以下で入力してください")
270
-
271
136
  private String name;
272
-
273
-
274
-
137
+
275
138
  @NotNull(message="メールアドレスを入力してください")
276
-
277
139
  @Email(message="メールアドレスのフォーマットで入力してください")
278
-
279
140
  private String email;
280
-
281
141
  @NotNull(message="問い合わせ内容を入力してください")
282
-
283
142
  private String contents;
284
-
285
-
286
-
143
+
287
144
  public InquiryForm() {
288
-
289
145
 
290
-
291
146
  }
292
-
293
147
  //getterとsetterは省略
294
-
295
-
296
-
148
+
297
149
  }
298
-
299
- ```
150
+ ```
300
-
301
151
  index_boot.html
302
-
303
152
  ```HTML
304
-
305
153
  <html xmlns:th="http://www.thymeleaf.org">
306
-
307
154
  <head>
308
-
309
155
  <link rel="icon" href="../../../../favicon.ico">
310
156
 
311
-
312
-
313
157
  <title>お問い合わせ一覧</title>
314
158
 
315
159
 
316
-
317
-
318
-
319
160
  </head>
320
-
321
161
  <body>
322
-
323
162
  <div class="starter-template">
324
-
325
163
  <h1 th:text="${title}">お問い合わせ一覧</h1>
326
-
327
164
  <p class="lead">お問い合わせ内容の一覧です。</p>
328
-
329
165
  </div>
330
-
331
166
 
332
-
333
167
  <table class="table table-striped">
334
-
335
168
  <thead>
336
-
337
169
  <tr>
338
-
339
170
  <th scope="col">ID</th>
340
-
341
171
  <th scope="col">名前</th>
342
-
343
172
  <th scope="col">メールアドレス</th>
344
-
345
173
  <th scope="col">問い合わせ内容</th>
346
-
347
174
  <th scope="col">日時</th>
348
-
349
175
  <th scope="col"></th>
350
-
351
176
  <th scope="col"></th>
352
-
353
177
  </tr>
354
-
355
178
  </thead>
356
-
357
179
  <tbody>
358
-
359
180
  <tr th:each="inquiry : ${inquiryList}">
360
-
361
181
  <td scope="row" th:text="${inquiry.id}">1</td>
362
-
363
182
  <td th:text="${inquiry.name}">name</td>
364
-
365
183
  <td th:text="${inquiry.email}">email</td>
366
-
367
184
  <td th:text="${inquiry.contents}">contents</td>
368
-
369
185
  <td th:text="${inquiry.created}">created</td>
370
-
371
186
  <td><a type="button" th:href="@{/inquiry/{id}(id=${inquiry.id})}">編集</a></td>
372
-
373
187
  <td>
374
-
375
188
  <form method="POST" th:action="@{/inquiry/delete}">
376
-
377
189
  <input type="hidden" name="inquiryId" th:value="${inquiry.id}">
378
-
379
190
  <input type="submit" value="削除" class="btn btn-primary">
380
-
381
191
  </form>
382
-
383
192
  </td>
384
-
385
193
  </tr>
386
194
 
387
-
388
-
389
195
  </tbody>
390
-
391
196
  </table>
392
197
 
393
198
 
394
-
395
-
396
-
397
199
  </main><!-- /.container -->
398
200
 
399
-
400
-
401
201
  </body>
402
-
403
202
  </html>
404
-
405
- ```
203
+ ```
406
-
407
-
408
204
 
409
205
  update.html
410
-
411
206
  ```HTML
412
-
413
207
  <html xmlns:th="http://www.thymeleaf.org"
414
-
415
208
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
416
-
417
209
  <head>
418
-
419
210
  <meta charset="utf-8">
420
-
421
211
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
422
-
423
212
  <meta name="description" content="">
424
-
425
213
  <meta name="author" content="">
426
-
427
214
  <link rel="icon" href="../../../../favicon.ico">
428
-
429
215
  <title>更新用フォーム</title>
430
216
 
431
-
432
-
433
217
  <!-- Bootstrap core CSS -->
434
-
435
218
  <link href="/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
436
219
 
437
-
438
-
439
220
  <!-- Custom styles for this template -->
440
-
441
221
  <link href="starter-template.css" th:href="@{/css/starter-template.css}" rel="stylesheet">
442
-
443
222
  </head>
444
223
 
445
-
446
-
447
224
  <body>
448
-
449
225
  <div th:replace="~{block/header::headerA}"></div>
450
-
451
226
  <main role="main" class="container">
452
227
 
453
-
454
-
455
228
  <h1 th:text="${title}">更新用フォーム</h1>
456
-
457
229
  <p th:if="${complete}" th:text="${complete}"></p>
458
-
459
230
  <form method="post" action="#" th:action="@{/inquiry/update}" th:object="${InquiryForm}">
460
-
461
-
462
-
231
+
463
232
  <label for="name">お名前</label>
464
-
465
233
  <input type="text" name="name" class="form-control" id="name" th:value="*{name}">
466
-
467
234
  <div class="text-danger mb-4" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
468
-
469
-
470
-
235
+
471
236
  <label for="email">メールアドレス</label>
472
-
473
237
  <input type="text" name="email" class="form-control" id="email" th:value="*{email}">
474
-
475
238
  <div class="text-danger mb-4" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
476
-
477
-
478
-
239
+
479
240
  <label for="contents">問い合わせ内容</label>
480
-
481
241
  <textarea name="contents" class="form-control" id="detail" rows="3" th:field="*{contents}"></textarea>
482
-
483
242
  <div class="text-danger mb-4" th:if="${#fields.hasErrors('contents')}" th:errors="*{contents}"></div>
484
-
485
-
486
-
243
+
487
244
  <input th:if="${inquiryId}" type="hidden" name="inquiryId" th:value="${inquiryId}">
488
-
489
245
  <button type="submit" style="margin-top:2px" class="btn btn-primary">送信</button><br>
490
-
491
-
492
-
246
+
493
247
  <a href="#" style="margin-top:10px" th:href="@{/inquiry}" class="btn btn-primary">戻る</a>
494
-
495
248
  </form><br>
496
-
497
249
  </main>
498
-
499
250
  </form>
500
-
501
251
  </div>
502
-
503
252
  </body>
504
-
505
253
  </html>
506
-
507
- ```
254
+ ```
508
-
509
255
  ### 試したこと
510
-
511
256
  update.htmlのソースコードに間違いがないか確認しましたがわかりませんでした。
512
257
 
513
-
514
-
515
258
  ### 補足情報(FW/ツールのバージョンなど)
516
-
517
259
  OS:macOS Big Sur 11.5.2
518
-
519
260
  使用ブラウザ:GoogleChrome
520
-
521
261
  Spring:2.5.4
522
-
523
262
  Java:OpenJDK 11.0.2
524
-
525
263
  DB:MySQL(AWSのRDS上で起動中)
526
264
 
527
-
528
-
529
265
  足りない情報等ありましたらコメントくださると幸いです。