回答編集履歴
1
追記
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
ブラウザに表示されたページのソースコードを見てみると```th:text```や```th:action```といったthymeleafの属性は消えていることが確認できると思います。
|
4
4
|
|
5
|
-
なのでJava
|
5
|
+
なのでJavaScriptを使って動的に追加する要素に```th:field```のようなthymeleafの属性を付けても動作しません。
|
6
6
|
|
7
7
|
|
8
8
|
|
@@ -15,3 +15,213 @@
|
|
15
15
|
<input type="text" id="testNo[0]" th:name="testNoList[0].testNo" onkeyup="fieldChanged();" onchange="fieldChanged();" th:field="*{testNoList[0].testNo}"></input>
|
16
16
|
|
17
17
|
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
**追記 2019/03/14**
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
少し調べてみましたが、Spring Bootやthymeleafの問題というよりもhtmlとJavaScriptの書き方の方に原因があったようです。
|
26
|
+
|
27
|
+
下記のようにhtmlとJavaScriptの書き方を変えれば配列を送信できました。
|
28
|
+
|
29
|
+
大きな変更点はtableタグの位置です。今まではformタグの外側にありましたが、それをformタグの内側へ移動させています。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
```html
|
34
|
+
|
35
|
+
<!DOCTYPE html>
|
36
|
+
|
37
|
+
<html xmlns:th="http://www.thymeleaf.org">
|
38
|
+
|
39
|
+
<head>
|
40
|
+
|
41
|
+
<meta charset="UTF-8">
|
42
|
+
|
43
|
+
<title>利用開始</title>
|
44
|
+
|
45
|
+
<script type="text/javascript">
|
46
|
+
|
47
|
+
function add() {
|
48
|
+
|
49
|
+
var inputCount = document.getElementsByClassName("testNo").length;
|
50
|
+
|
51
|
+
var input = document.createElement("input");
|
52
|
+
|
53
|
+
input.setAttribute("type", "text");
|
54
|
+
|
55
|
+
input.setAttribute("id", "testNoList" + inputCount + ".testNo");
|
56
|
+
|
57
|
+
input.setAttribute("class", "testNo");
|
58
|
+
|
59
|
+
input.setAttribute("name", "testNoList[" + inputCount + "].testNo")
|
60
|
+
|
61
|
+
input.setAttribute("value", "");
|
62
|
+
|
63
|
+
var ele = document.createElement("div");
|
64
|
+
|
65
|
+
ele.appendChild(document.createTextNode(inputCount + ". "));
|
66
|
+
|
67
|
+
ele.appendChild(input);
|
68
|
+
|
69
|
+
document.getElementById("piyo").appendChild(ele);
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
</script>
|
74
|
+
|
75
|
+
</head>
|
76
|
+
|
77
|
+
<body>
|
78
|
+
|
79
|
+
<form name="form1" action="#" method="post" th:action="@{/Use/submit}" th:object="${historyForm}">
|
80
|
+
|
81
|
+
<table>
|
82
|
+
|
83
|
+
<tr>
|
84
|
+
|
85
|
+
<td>~userId の入力テキストボックス</td>
|
86
|
+
|
87
|
+
<td>
|
88
|
+
|
89
|
+
<input type="text" th:field="*{userId}"/>
|
90
|
+
|
91
|
+
</td>
|
92
|
+
|
93
|
+
</tr>
|
94
|
+
|
95
|
+
<tr>
|
96
|
+
|
97
|
+
<td>~itemID の入力テキストボックス</td>
|
98
|
+
|
99
|
+
<td>
|
100
|
+
|
101
|
+
<input type="text" th:field="*{itemId}"/>
|
102
|
+
|
103
|
+
</td>
|
104
|
+
|
105
|
+
</tr>
|
106
|
+
|
107
|
+
<tr id="testNo_input">
|
108
|
+
|
109
|
+
<td class="login_field">テストコード <a href="#" id="addInput" tabIndex="-1">+追加</a></td>
|
110
|
+
|
111
|
+
<td>
|
112
|
+
|
113
|
+
<div>
|
114
|
+
|
115
|
+
0. <input class="testNo" type="text" th:field="*{testNoList[0].testNo}"/>
|
116
|
+
|
117
|
+
</div>
|
118
|
+
|
119
|
+
<div id="piyo"></div>
|
120
|
+
|
121
|
+
</td>
|
122
|
+
|
123
|
+
</tr>
|
124
|
+
|
125
|
+
<tr>
|
126
|
+
|
127
|
+
<td>~date の入力テキストボックス</td>
|
128
|
+
|
129
|
+
<td>
|
130
|
+
|
131
|
+
<input type="text" th:field="*{date}"/>
|
132
|
+
|
133
|
+
</td>
|
134
|
+
|
135
|
+
</tr>
|
136
|
+
|
137
|
+
<tr>
|
138
|
+
|
139
|
+
<td colspan="2">
|
140
|
+
|
141
|
+
<input type="submit" value="この内容で登録する" id="submit">
|
142
|
+
|
143
|
+
</td>
|
144
|
+
|
145
|
+
</tr>
|
146
|
+
|
147
|
+
</table>
|
148
|
+
|
149
|
+
</form>
|
150
|
+
|
151
|
+
<script type="text/javascript">
|
152
|
+
|
153
|
+
document.getElementById("addInput").addEventListener("click", function (e) {
|
154
|
+
|
155
|
+
add();
|
156
|
+
|
157
|
+
}, false);
|
158
|
+
|
159
|
+
</script>
|
160
|
+
|
161
|
+
</body>
|
162
|
+
|
163
|
+
</html>
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
Java(Spring Boot)側のコードは修正していませんが、確認用に下記のコードを追記しています。
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
```java
|
174
|
+
|
175
|
+
@PostMapping("Use/submit")
|
176
|
+
|
177
|
+
public ModelAndView create(@Valid @ModelAttribute HistoryForm historyForm, ModelAndView mv) {
|
178
|
+
|
179
|
+
System.out.println(historyForm);
|
180
|
+
|
181
|
+
System.out.println("size:" + historyForm.getTestNoList().size());
|
182
|
+
|
183
|
+
historyForm.getTestNoList().forEach(a -> System.out.println("testNo:" + a.getTestNo()));
|
184
|
+
|
185
|
+
mv.setViewName("history_confirm");
|
186
|
+
|
187
|
+
return mv;
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
```
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
この状態で動かしてみると
|
196
|
+
|
197
|
+

|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
コンソールには以下の内容が出力されます。
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
```
|
206
|
+
|
207
|
+
HistoryForm(userId=100, itemId=101, testNoList=[HistoryForm.TestNoList(testNo=a), HistoryForm.TestNoList(testNo=bb), HistoryForm.TestNoList(testNo=ccc), HistoryForm.TestNoList(testNo=dddd), HistoryForm.TestNoList(testNo=eeeee), HistoryForm.TestNoList(testNo=ffffff)], date=103)
|
208
|
+
|
209
|
+
size:6
|
210
|
+
|
211
|
+
testNo:a
|
212
|
+
|
213
|
+
testNo:bb
|
214
|
+
|
215
|
+
testNo:ccc
|
216
|
+
|
217
|
+
testNo:dddd
|
218
|
+
|
219
|
+
testNo:eeeee
|
220
|
+
|
221
|
+
testNo:ffffff
|
222
|
+
|
223
|
+
```
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
以上ご確認ください。
|