Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表单支持bean中的Spring转换_Spring_Spring Mvc_Spring Data Jpa_Thymeleaf_Java Time - Fatal编程技术网

表单支持bean中的Spring转换

表单支持bean中的Spring转换,spring,spring-mvc,spring-data-jpa,thymeleaf,java-time,Spring,Spring Mvc,Spring Data Jpa,Thymeleaf,Java Time,我实际上有两个问题。两者发生在相同的情况下,如下所示: 我正在使用spring和thymeleaf,我想在服务器上发布一个表单,这很好,但服务器无法将提交的一些数据转换为我的bean的属性类型 表格: <form th:action="@{/demo}}" th:object="${myBean}" method="post"> <label>date</label> <input type="date" th:field="*{date

我实际上有两个问题。两者发生在相同的情况下,如下所示:

我正在使用spring和thymeleaf,我想在服务器上发布一个表单,这很好,但服务器无法将提交的一些数据转换为我的bean的属性类型

表格:

<form th:action="@{/demo}}" th:object="${myBean}" method="post">
    <label>date</label>
    <input type="date" th:field="*{date}">
    <label>type</label>
    <select th:filed="*{type}">
        <option th:each="type: ${types}" th:value="${type.id}" th:text="${type.name}"></option>
    </select>
    <button type="submit">Submit</button>
</form>
问题:

  • 无法将日期输入的值转换为java.time.ZonedDateTime
  • select的值(将作为数字发布)无法转换为MyType类型的对象。我会扩展它,因为MyType是一个JPA实体,并且为它定义了
    org.springframework.data.repository.crudepository
  • 如果你们能帮助我,我将非常高兴。

    我自己解决了这个问题

    日期输入的值作为字符串发送,即“2017-12-31”,以便能够在我的表单中使用ZonedDateTime。我必须注册一个自定义转换器。代码如下:

    public class ZonedDateTimeConverter implements Converter<String, ZonedDateTime> {
    @Override
    public ZonedDateTime convert(String source) {
        return ZonedDateTime.of(LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd")), LocalTime.of(00, 00),
                ZoneId.systemDefault());
    }
    
    对于第二个问题,解决方案甚至更简单,我要做的唯一一件事就是用
    @EnableSpringDataWebSupport
    注释我的应用程序,这反过来又为我的实体bean注册了一些转换器

    public class ZonedDateTimeConverter implements Converter<String, ZonedDateTime> {
    @Override
    public ZonedDateTime convert(String source) {
        return ZonedDateTime.of(LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd")), LocalTime.of(00, 00),
                ZoneId.systemDefault());
    }
    
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new ZonedDateTimeConverter());
    }