【问题标题】:Form input type DateTime using Thymeleaf + Spring MVC issue使用 Thymeleaf + Spring MVC 问题的表单输入类型 DateTime
【发布时间】:2019-09-30 22:51:04
【问题描述】:

我正在努力以 Thymeleaf 形式使用 Java LocalDateTime scheduleDateTime 并将其取回以将其保存在数据库中。我收到以下错误消息:

Bean 属性 'campaignExecution' 不可读或有无效的 getter 方法:getter 的返回类型是否与 setter 的参数类型匹配?

这确实是由于日期时间字段。如果我从 html 表单中删除它,一切正常,我会看到控制器中创建的对象的数据。

我的对象包含以下内容以及返回类型 (LocalDateTime) 的 getter 和 setter:

@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") 
private LocalDateTime scheduledDateTime;

我的控制器将值初始化为 now()

@RequestMapping({ "/campaign1"})
public String requestCampaign1(Model model) {       
CampaignExecution ce = new CampaignExecution();
LocalDateTime localDateTime = LocalDateTime.now();
ce.setScheduledDateTime(localDateTime);
model.addAttribute("campaignExecution", ce);

这是表格:

    <form id="f" name="f" th:action="@{/campaign1}"
        th:object="${campaignExecution}" method="post">
        <div>
            Schedule a date :
            <input type="datetime-local" th:field=*{campaignExecution.scheduledDateTime} />
        </div>
        <hr>
        <div>Parameters</div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>

【问题讨论】:

    标签: spring spring-mvc datetime thymeleaf


    【解决方案1】:

    比我想象的要容易得多。对于 th:field,您不能使用多个对象,而只能使用“表单对象”。我使用的是完整的限定符:对象名称.属性名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-16
      • 2016-04-22
      • 2017-03-18
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      相关资源
      最近更新 更多