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

回答編集履歴

1

追記

2019/03/14 14:26

投稿

rubytomato
rubytomato

スコア1752

answer CHANGED
@@ -1,9 +1,114 @@
1
1
  thymeleafを使って作成したテンプレートファイル(*.html)はサーバーサイドでレンダリングされた後、ブラウザで表示されます。
2
2
  ブラウザに表示されたページのソースコードを見てみると```th:text```や```th:action```といったthymeleafの属性は消えていることが確認できると思います。
3
- なのでJavascriptを使って動的に追加する要素に```th:field```のようなthymeleafの属性を付けても動作しません。
3
+ なのでJavaScriptを使って動的に追加する要素に```th:field```のようなthymeleafの属性を付けても動作しません。
4
4
 
5
5
  テンプレートの下記の行が最終的にどのようなhtmlになっているか確認し、これと同じようなinputタグをJavascriptで組み立ててください。
6
6
 
7
7
  ```
8
8
  <input type="text" id="testNo[0]" th:name="testNoList[0].testNo" onkeyup="fieldChanged();" onchange="fieldChanged();" th:field="*{testNoList[0].testNo}"></input>
9
- ```
9
+ ```
10
+
11
+ **追記 2019/03/14**
12
+
13
+ 少し調べてみましたが、Spring Bootやthymeleafの問題というよりもhtmlとJavaScriptの書き方の方に原因があったようです。
14
+ 下記のようにhtmlとJavaScriptの書き方を変えれば配列を送信できました。
15
+ 大きな変更点はtableタグの位置です。今まではformタグの外側にありましたが、それをformタグの内側へ移動させています。
16
+
17
+ ```html
18
+ <!DOCTYPE html>
19
+ <html xmlns:th="http://www.thymeleaf.org">
20
+ <head>
21
+ <meta charset="UTF-8">
22
+ <title>利用開始</title>
23
+ <script type="text/javascript">
24
+ function add() {
25
+ var inputCount = document.getElementsByClassName("testNo").length;
26
+ var input = document.createElement("input");
27
+ input.setAttribute("type", "text");
28
+ input.setAttribute("id", "testNoList" + inputCount + ".testNo");
29
+ input.setAttribute("class", "testNo");
30
+ input.setAttribute("name", "testNoList[" + inputCount + "].testNo")
31
+ input.setAttribute("value", "");
32
+ var ele = document.createElement("div");
33
+ ele.appendChild(document.createTextNode(inputCount + ". "));
34
+ ele.appendChild(input);
35
+ document.getElementById("piyo").appendChild(ele);
36
+ }
37
+ </script>
38
+ </head>
39
+ <body>
40
+ <form name="form1" action="#" method="post" th:action="@{/Use/submit}" th:object="${historyForm}">
41
+ <table>
42
+ <tr>
43
+ <td>~userId の入力テキストボックス</td>
44
+ <td>
45
+ <input type="text" th:field="*{userId}"/>
46
+ </td>
47
+ </tr>
48
+ <tr>
49
+ <td>~itemID の入力テキストボックス</td>
50
+ <td>
51
+ <input type="text" th:field="*{itemId}"/>
52
+ </td>
53
+ </tr>
54
+ <tr id="testNo_input">
55
+ <td class="login_field">テストコード <a href="#" id="addInput" tabIndex="-1">+追加</a></td>
56
+ <td>
57
+ <div>
58
+ 0. <input class="testNo" type="text" th:field="*{testNoList[0].testNo}"/>
59
+ </div>
60
+ <div id="piyo"></div>
61
+ </td>
62
+ </tr>
63
+ <tr>
64
+ <td>~date の入力テキストボックス</td>
65
+ <td>
66
+ <input type="text" th:field="*{date}"/>
67
+ </td>
68
+ </tr>
69
+ <tr>
70
+ <td colspan="2">
71
+ <input type="submit" value="この内容で登録する" id="submit">
72
+ </td>
73
+ </tr>
74
+ </table>
75
+ </form>
76
+ <script type="text/javascript">
77
+ document.getElementById("addInput").addEventListener("click", function (e) {
78
+ add();
79
+ }, false);
80
+ </script>
81
+ </body>
82
+ </html>
83
+ ```
84
+
85
+ Java(Spring Boot)側のコードは修正していませんが、確認用に下記のコードを追記しています。
86
+
87
+ ```java
88
+ @PostMapping("Use/submit")
89
+ public ModelAndView create(@Valid @ModelAttribute HistoryForm historyForm, ModelAndView mv) {
90
+ System.out.println(historyForm);
91
+ System.out.println("size:" + historyForm.getTestNoList().size());
92
+ historyForm.getTestNoList().forEach(a -> System.out.println("testNo:" + a.getTestNo()));
93
+ mv.setViewName("history_confirm");
94
+ return mv;
95
+ }
96
+ ```
97
+
98
+ この状態で動かしてみると
99
+ ![イメージ説明](e4b32541f4995c79d97cf30aaa03afb0.png)
100
+
101
+ コンソールには以下の内容が出力されます。
102
+
103
+ ```
104
+ 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)
105
+ size:6
106
+ testNo:a
107
+ testNo:bb
108
+ testNo:ccc
109
+ testNo:dddd
110
+ testNo:eeeee
111
+ testNo:ffffff
112
+ ```
113
+
114
+ 以上ご確認ください。