2017-01-23 66 views
1

我遇到了麻煩:/我有一個對象具有另一個對象的列表,我想通過選擇框和輸入來更改此列表,但是當我提交時對此,該列表是空如何通過百里香形式更新對象與對象列表的關係

這裏是我的代碼(模型,thymelaf形式,控制器):

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

你有一個'@ GetMapping',你可以將'config' bean添加到模型中? – bphilipnyc

+0

嗨,我編輯了我的問題,並添加了將bean配置添加到模型的控制器的一部分。 – user1604064

+0

如果你在你的post方法中做'configVM.getId()',打印一個值嗎? (你也可以用調試器來完成。)另外,沒有例外,對嗎? – bphilipnyc

回答

0

我發現了一個solution..the形式必須是這樣的:

<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"/>