【问题标题】:How to use joda time DateTime with Thymeleaf如何在 Thymeleaf 中使用 joda time DateTime
【发布时间】:2015-05-22 20:42:17
【问题描述】:

我有以下控制器:

@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)。我看到可以使用转换器/格式化程序,但我正在使用 spring-boot,我真的不知道该怎么做,因为所有配置都是自动加载的。我尝试了两种输入类型=“文本”/“日期时间”,都没有工作 非常感谢您的帮助。

【问题讨论】:

    标签: datetime converter spring-boot jodatime thymeleaf


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,我已经成功地打印了一个现有的 JodaTime 日期:th:text="${#joda.mediumDate(date)}"。但是我找不到如何使用 任何想法以表格形式进行分配?
    • 小心在商业项目中使用jodatime-thymeleaf-dialect,许可证是GPL
    猜你喜欢
    • 2013-05-03
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    相关资源
    最近更新 更多