2017-01-23 14 views
1

У меня есть проблема:/У меня есть один объект, который имеет список других объектов, и я хочу изменить этот список через поле выбора и входы, но когда я отправлю этот список пустКак обновить объект по отношению к списку объектов с помощью формы thymeleaf

Вот мой код (модель, 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"; 
    } 

есть ли у вас какие-либо идеи, как передать измененный список в объекте?

EDIT: Добавление контроллера часть, которая будет показывать форму:

@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' к модели? – bphilipnyc

+0

Привет, я редактировал свой вопрос и добавил часть контроллера, которая добавит конфигурацию bean для модели. – user1604064

+0

Если вы используете 'configVM.getId()' в своем методе post, выводится ли значение? (Вы также можете перейти с помощью отладчика.) Кроме того, никаких исключений, правильно? – 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"/>