如何使用joda time DateTime与Thymeleaf

时间:2015-03-19 16:07:28

标签: datetime converter spring-boot jodatime thymeleaf

我有以下控制器:

@RequestMapping(value="/usage", method=RequestMethod.GET)
public String usage(Model model) {
    CallDataRecordLine calldatarecordline = new CallDataRecordLine();
    model.addAttribute("calldatarecordline",calldatarecordline);
    return "usage";
}
@RequestMapping(value="/usage", method=RequestMethod.POST)
public String greetingSubmit(@ModelAttribute CallDataRecordLine calldatarecordline, Model model) {
    model.addAttribute("calldatarecordline",calldatarecordline);
    return "result";
}

该实体描述如下

public class CallDataRecordLine {

    int chargedQuantity;
    String fromNumber;
    String toNumber;
    DateTime dt;
    String usage;
    String chargeableQuantity;
    //getters/setters
}

链接到控制器的视图

<form id="calldatarecordForm" class="form-horizontal" role="form"
    action="#" th:action="@{/usage}" th:object="${calldatarecordline}"
    method="post">

    <input type="text" th:field="*{fromNumber}" class="form-control" /> <input
        type="text" th:field="*{toNumber}" class="form-control" /> <input
        type="datetime" th:field="*{dt}" class="form-control" /> <input
        type="text" th:field="*{chargedQuantity}" class="form-control" /> <input
        type="text" th:field="*{usage}" class="form-control" /> <input
        type="text" th:field="*{chargeableQuantity}" class="form-control" />


    <button type="submit">Test</button>
</form>

显然,th:field无法转换jodaTime DateTime(我尝试删除dt属性,并创建了CallDataRecordLine correclty)。我看到可以使用转换器/格式化器,但我使用的是spring-boot,而且由于所有配置都是自动加载的,所以我真的不知道怎么做。我尝试了输入类型=&#34;文字&#34; /&#34;日期时间&#34;,没有工作 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这里看看这个jodatime方言https://github.com/gcwilliams/jodatime-thymeleaf-dialect

我也会根据w3schools类型保留type =“text”任何浏览器都不支持日期时间http://www.w3schools.com/html/html_form_input_types.asp