【问题标题】:Date type with ThymeleafThymeleaf 的日期类型
【发布时间】:2021-01-24 08:05:28
【问题描述】:

使用 Thymeleaf 我在我的 html 模板的表单 POST 方法中收到此错误:

There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='miembro'. Error count: 2
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'miembro' on field 'fechaIngreso': rejected value [2020-10-12]; codes [typeMismatch.miembro.fechaIngreso,typeMismatch.fechaIngreso,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [miembro.fechaIngreso,fechaIngreso]; arguments []; default message [fechaIngreso]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'fechaIngreso'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2020-10-12"]
Field error in object 'miembro' on field 'fechaNacimiento': rejected value [1992-11-12]; codes [typeMismatch.miembro.fechaNacimiento,typeMismatch.fechaNacimiento,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [miembro.fechaNacimiento,fechaNacimiento]; arguments []; default message [fechaNacimiento]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'fechaNacimiento'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "1992-11-12"]

似乎是日期解析的问题。被拒绝的值在 yyyy-MM-dd 中,但我已经在项目内的每个 Date 属性中插入了该格式的注释。示例:

@Entity
public class Miembro extends Persona {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int idMiembro;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Temporal(TemporalType.DATE)
    private Date fechaIngreso;

此外,SQL 列类型正确映射为“日期”类型。

有问题的 Thymeleaf 部分代码:

<div class="jumbotron" th:if="${habilitarIngreso}">
  <form th:action="principal" th:object="${miembroP}" method="post">
    <label>Nombre</label>
    <input type="text" th:field="*{nombre}" 
    placeholder="Ingrese nombre"/>
     <label>Apellido</label>
     <input type="text" th:field="*{apellido}" 
    placeholder="Ingrese apellido"/>
      <label>D.N.I</label>
     <input type="number" th:field="*{dni}" 
    placeholder="Ingrese dni"/>
          <label>Teléfono</label>
     <input type="number" th:field="*{telefono}" 
    placeholder="Ingrese teléfono"/>
    <label>Dirección</label>
    <input type="text" th:field="*{direccion}" 
   placeholder="Ingrese dirección"/>  
   <label>Fecha Nacimiento</label>
     <input type="date" th:field="*{fechaNacimiento}" 
    placeholder="Ingrese fecha de nacimiento"/>   
    <label>Fecha de ingreso</label>
     <input type="date" th:field="*{fechaIngreso}" 
    placeholder="Ingrese fecha de ingreso"/>
      <input type="submit" value="Enviar"/>
  </form>
</div>

你能告诉我发生了什么吗?

【问题讨论】:

  • 乍一看似乎没有什么问题。您是否有显示问题的在线示例?

标签: java spring date thymeleaf


【解决方案1】:

我通过将所有日期变量从 Date 更改为 LocalDate 来修复它。不知道为什么这确实有效,似乎 Thymeleaf 与 Date 库不太相配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多