我遇到了麻烦:/我有一个对象有另一个对象的列表,我想通过选择框和输入更改此列表,但是当我提交此列表时,列表为空
这是我的代码(模型,百里香形式,控制器):
public class BetConfigVM {
private long id;
private String name;
private List<BetPriorityVM> betPriorities;
....getters and setters
}
public class BetPriorityVM {
private long id;
private CourseType courseType;
private BigDecimal minCourse;
private BigDecimal maxCourse;
....getters and setters
}
<form action="#" th:action="@{/betConfigSaveOrUpdate}" th:object="${config}" method="post">
<input type="text" th:field="*{name}" />
<span th:field="*{betPriorities}" th:each="prio : *{betPriorities}">
<select>
<option th:each="type : ${T(com.model.database.CourseType).values()}"
th:value="${type}" th:text="${type}" th:selected="${prio.courseType == type}">
</option>
</select>
<input type="text" th:field="${prio.minCourse}" />
<input type="text" th:field="${prio.maxCourse}" />
</span>
<input type="submit" th:value="Save" name="action"/>
</form>
@RequestMapping(value = "/betConfigSaveOrUpdate", method = RequestMethod.POST)
public String saveOrUpdateUser(@ModelAttribute("config") BetConfigVM configVM, @RequestParam String action, Model model) {
System.out.println(configVM.getName());
System.out.println("Size " + configVM.getBetPriorities().size());
model.addAttribute("config", configVM);
return "user/betConfigEdit";
}
您是否知道如何在对象中传递更改后的列表?
编辑: 添加将显示表单的控制器部分:
@RequestMapping(value = "/changeBetConfig", method = RequestMethod.GET)
public String changeBetConfig(Model model, @RequestParam("id") long id) {
BetConfig betConfig = betConfigRepository.findById(id);
BetConfigVM configVM = new BetConfigVM(betConfig);
model.addAttribute("config", configVM);
return "user/betConfigEdit";
}
答案 0 :(得分:1)
我找到了一个解决方案......表单必须如下所示:
<form action="#" th:action="@{/betConfigSaveOrUpdate}" th:object="${config}" method="post">
<input type="text" th:field="*{name}" />
<span th:each="prio, rowStat : *{betPriorities}">
<select th:field="*{betPriorities[__${rowStat.index}__].courseType>
<option th:each="type : ${T(com.model.database.CourseType).values()}"
th:value="${type}" th:text="${type}" th:selected="${prio.courseType == type}">
</option>
</select>
<input type="text" th:field="*{betPriorities[__${rowStat.index}__].minCourse}" />
<input type="text" th:field="*{betPriorities[__${rowStat.index}__].maxCourse}" />
</span>
<input type="submit" th:value="Save" name="action"/>