前提
datalistで選択したデータをFormクラスに格納したいのですが、現状Null判定になっています。
datalistを使用していないinputの部分はフォームクラスにデータを格納できていることをデバックで確認しています。
実現したいこと
datalistで選択したデータをFormクラスに格納する
写真の矢印部分のデータに格納したい
発生している問題・エラーメッセージ
該当のソースコード
Thymeleaf
HTML
1<body> 2<div layout:fragment="content"> 3 <h1 th:text="${title}">在庫入力画面</h1> 4 <p th:if="${complete}" th:text="${complete}"></p> 5 <form method="POST" th:action="@{/inventory/insert}" th:object="${inventoryForm}"> 6 <table> 7 <tr> 8 <td><label>商品名(選択してください):</label></td> 9 <td> 10 <input type="text" name="name" list="productId"/> 11 <datalist id="productId"> 12 <option th:each="product : *{prodList}" th:inline="text" 13 th:filed="${product.id}" th:utext="${product.name}" th:selected="${product.id} == *{productId}"> 14 </option> 15 </datalist> 16 </td> 17 <td><div th:if="${#fields.hasErrors('productId')}" th:errors="*{productId}"></div></td> 18 </tr> 19 <tr> 20 <td><label>在庫数:</label></td> 21 <td><input type="number" step="0.1" name="quantity" th:value="*{quantity}" /></td> 22 <td><div th:if="${#fields.hasErrors('quantity')}" th:errors="*{quantity}"></div></td> 23 </tr> 24 <tr> 25 <td><label>日付:</label></td> 26 <td><input type="date" name="reportDate" th:value="*{reportDate}" /></td> 27 <td><div th:if="${#fields.hasErrors('reportDate')}" th:errors="*{reportDate}"></div></td> 28 </tr> 29 </table> 30 <input type="hidden" name="productId" th:value="${productId}"> 31 <input type="submit" class="btn btn-primary" value="送信" /> 32 </form> 33</div> 34</body>
Formクラス
Java
1public class InventoryForm { 2 3 4 @NotNull(message ="商品名を選択してください。") 5 private Integer productId; 6 7 8 @NotNull(message = "数値を入力してください。") 9 @PositiveOrZero(message = "正の数を入力してください。") 10 private BigDecimal quantity; 11 12 13 @NotNull(message = "日付を設定してください。") 14 @DateTimeFormat(pattern = "yyyy-MM-dd") 15 @PastOrPresent(message = "日付が未来に設定されています。") 16 private LocalDate reportDate; 17 18 private ArrayList<Product> prodList = new ArrayList<>(); 19}
Entityクラス
Java
1@Data 2public class Product { 3 4 private int id; 5 6 private String name; 7 8 private List<Inventory> inventorys; 9} 10
試したこと
https://hitori-goto.com/2021/06/10/springboot-thymeleaf-use-selected-and-disabled-in-select-option/
この記事を参考にコーディングしました。
補足情報(FW/ツールのバージョンなど)
java11, spring boot, Thymeleaf
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。