在Thymeleaf中绑定对象 - SpringBoot

时间:2017-03-19 18:20:12

标签: html spring spring-mvc spring-boot thymeleaf

我使用Spring Initializr生成了一个Spring Boot Web应用程序,使用嵌入式Tomcat + Thymeleaf模板引擎,并将package打包为可执行的JAR文件。

使用的技术:

Spring Boot 1.4.2.RELEASE,Spring 4.3.4.RELEASE,Thymeleaf 2.1.5.RELEASE,Tomcat Embed 8.5.6,Maven 3,Java 8

我有这个班级

public class AlarmNotification {

    private Long id;

    private Action action;

    private Application aplication;

    private Device device;

    private TimeLapse timeLapse;

    private Guardian guardian;

    private List<SystemAlarm.Level> level;

    ...
}

和这个模板

<form class="form-style-9" action="#" th:action="@{/alarmNotification/save}" th:object="${alarmNotification}" method="post">
    <ul>
    <li>

       <input type="hidden" th:field="*{id}"       />

        <select name="device" class="field-style field-split align-left">
        <option value="ANDROID">ANDROID</option>
        <option value="IOS">IOS</option>
    </select>
       <select th:field="*{guardian}">
         <option th:each="guardian : ${guardians}" 
             th:value="${guardian.id}" 
             th:text="${guardian.name}"></option>
    </select>   
    </li>   
    <li>
       <select th:field="*{timeLapse}">
         <option th:each="timeLapse : ${timeLapses}" 
            th:value="${timeLapse.id}"
             th:text="${timeLapse.description}"></option>
    </select>   
    </li>
     <li>
       <select th:field="*{level}" multiple="multiple">
         <option th:each="level : ${alarmLevels}" 
             th:value="${level}" 
             th:text="${level}"></option>
    </select> 
    </li>
    <li>        
    </li>
    <li>
        <input type="submit" value="SAVE" />
    </li>
    </ul>
</form>

和控制器:

@RequestMapping(value = { "/save" }, method = RequestMethod.POST)
    public String saveAlarmNotification (@Valid AlarmNotification alarmNotification, BindingResult bindingResult, Model model) throws DataAccessException, SQLException {

        System.out.println  ("<-------------------------------------->");
        System.out.println  (alarmNotification);
        System.out.println  ("<-------------------------------------->");
..
}

但是对象alarmNotification的所有值都是null,除了设备和级别:

 AlarmNotification [id=null, action=null, aplication=null,
 device=Device [id=null, deviceKey=ANDROID], 
timeLapse=null, guardian=null, 
level=[HIGH, MEDIUM, LOW]]

1 个答案:

答案 0 :(得分:4)

我认为在你的select标签中th:value不正确,id未正确设置。

即对于监护人来说应该是这样的

<select th:field="*{guardian}">
      <option th:each="guardian : ${guardians}" 
             th:value="${guardian.id}" 
             th:text="${guardian.name}"></option>
</select>

表示timeLapse应该像那样声明

<select th:field="*{timeLapse}">
     <option th:each="timeLapse : ${timeLapses}" 
         th:value="${timeLapse.id}" 
         th:text="${timeLapse.description}"></option>
</select>  

并且对于级别我认为你传递了错误的字段,即它可能看起来像那样

<select th:field="*{level}" multiple="multiple">
             <option th:each="level : ${alarmLevels}" 
                 th:value="${level}" 
                 th:text="${level}"></option>
        </select>

我希望这会有所帮助。