回答編集履歴
2
説明を追加
test
CHANGED
@@ -1,7 +1,41 @@
|
|
1
|
-
th:objectの名前を、先頭小文字の「contactForm」にすると解決しないでしょうか。
|
1
|
+
th:objectの名前を、先頭小文字の「contactForm」にすると解決(進展)しないでしょうか。
|
2
2
|
|
3
3
|
```html
|
4
|
+
<!-- 75行目 -->
|
4
5
|
<form ... th:object="${contactForm}">
|
5
6
|
```
|
6
|
-
※引数をContactForm personFormとした場合、DIコンテナに登録されるのは、先頭小文字のcontactForm
|
7
|
+
※引数をContactForm personFormとした場合、DIコンテナに登録されるのは、先頭小文字のcontactFormとなります
|
7
8
|
|
9
|
+
## 説明
|
10
|
+
|
11
|
+
[公式ドキュメントに書いてあるように](https://spring.pleiades.io/spring-framework/reference/web/webmvc/mvc-controller/ann-methods/arguments.html)、@ModelAttributeはオプションです。このため、以下は全て同じ動作になります。
|
12
|
+
|
13
|
+
```java
|
14
|
+
// 以下は全て@ModelAttributeが付与されます
|
15
|
+
public String contactForm(ContactForm personForm) {...} //省略した場合@ModelAttribute ContactForm personFormになる
|
16
|
+
public String contactForm(@ModelAttribute ContactForm personForm) {...}
|
17
|
+
public String contactForm(@ModelAttribute("contactForm") ContactForm personForm) {...}
|
18
|
+
// ※Javaの命名規約で、メソッド名は小文字にします
|
19
|
+
```
|
20
|
+
|
21
|
+
また、@ModelAttributeを使った場合はmodelを省略できますが、これを全部書くと以下のようになります。
|
22
|
+
|
23
|
+
```java
|
24
|
+
@GetMapping("/work3/form")
|
25
|
+
public String contactForm(@ModelAttribute ContactForm personForm, Model model) {
|
26
|
+
model.addAttribute("contactForm", personForm); //省略できます
|
27
|
+
return "/work3/form";
|
28
|
+
}
|
29
|
+
|
30
|
+
// 全て省略した場合でも、上記と同じになります
|
31
|
+
@GetMapping("/work3/form")
|
32
|
+
public String contactForm(ContactForm personForm) {
|
33
|
+
return "/work3/form";
|
34
|
+
}
|
35
|
+
```
|
36
|
+
以上のことから、Thymeleaf側で先頭大文字の「ContactForm」にすると動作させることができません。
|
37
|
+
|
38
|
+
```html
|
39
|
+
<!-- th:objectが動作しない -->
|
40
|
+
<form ... th:object="${ContactForm}">
|
41
|
+
```
|
1
一部修正
test
CHANGED
@@ -3,5 +3,5 @@
|
|
3
3
|
```html
|
4
4
|
<form ... th:object="${contactForm}">
|
5
5
|
```
|
6
|
-
※
|
6
|
+
※引数をContactForm personFormとした場合、DIコンテナに登録されるのは、先頭小文字のcontactFormになります
|
7
7
|
|