【问题标题】:Spring boot / Thymeleaf - Getting Time from inputSpring boot / Thymeleaf - 从输入中获取时间
【发布时间】:2017-07-24 22:28:09
【问题描述】:

我正在尝试使用表单中的单独输入来获取日期和时间。 到目前为止,获取日期是有效的,但是使用获取时间(小时和分钟) 导致错误。 这是我的实体类的一部分:

@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date start_date;

@Temporal(TemporalType.TIME)
@DateTimeFormat(pattern = "HH:mm")
private Date start_hour;

这是我用来从用户那里获取数据的表单部分:

<div class="row">
    <div class="col-6">
        <label th:attr="for='start_date_'+${subject.id}">Start Date</label>
        <input class="form-control" type="date" th:attr="id='start_date_'+${subject.id}" th:field="*{start_date}"/>
     </div>
     <div class="col-6">
         <label th:attr="for='start_time_'+${subject.id}">Start hour</label>
         <input class="form-control" type="time" th:attr="id='start_time_'+${subject.id}" th:field="*{start_hour}"/>
      </div>
</div>

我不断收到错误:

There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='lesson'. Error count: 1

我分别测试了日期输入和时间输入,是时间输入给我带来了麻烦。

【问题讨论】:

  • 查看@InitBinder文档,对解决你的问题很有帮助

标签: spring spring-boot thymeleaf


【解决方案1】:

尝试将数据类型改为

java.sql.Datejava.sql.Time

或者如果您使用的是 Java 8

@Temporal(TemporalType.DATE)
@DateTimeFormat(iso = ISO.DATE)
private LocalDate start_date;

@Temporal(TemporalType.TIME)
@DateTimeFormat(iso = ISO.TIME)
private LocalDateTime start_hour;

【讨论】:

    【解决方案2】:

    看到您的错误可能是由Validation Layer 引起的,它拒绝了您的对象。我建议您尝试调试或打印错误列表,看看是什么错误拒绝了您的请求。

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 1970-01-01
      • 2021-08-01
      • 2015-01-30
      • 1970-01-01
      • 2021-12-25
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多