使用表单字段绑定持续时间对象

时间:2016-11-10 17:08:37

标签: spring-mvc thymeleaf

我的课程类型为持续时间:

class MyEntity{

private Duration duration;
//getters, setters
}

现在,在我的百里香模板中,我想要有小时,分钟和秒的表格:

<form method="post" th:action="@{/add}" th:object="${entity}">
     <input type="number" th:field="???" min="0" />
     <input type="number" th:field="???" min="0" max="59" />
     <input type="number" th:field="???" min="0" max="59" />
</form>

在我的控制器中,我希望有类似的东西:

@PostMapping("/save")
public String saveTime(@ModelAttribute MyEntity entity) {
    timeRepo.save(timeData);
    return "redirect:/";
}

是否有可能使用thymeleaf来绑定除了字符串和数字之外还有其他对象类型的模型,或者我必须创建DTO并自己关心包装/解包数据?

1 个答案:

答案 0 :(得分:1)

尝试使用转换器,如果您认为这个概念很新,此链接可能会对您有所帮助:

Spring MVC - Binding Java Backing Objects with Custom Converters

相关问题