Input type=“date” thymeleaf

前端 未结 2 433
温柔的废话
温柔的废话 2020-12-17 18:29

I need to add date to my entity and let the user set it in web form. This field needs to have today\'s date filled in by default.

1. 

        
相关标签:
2条回答
  • 2020-12-17 19:03

    Use string instead of Date

    @DateTimeFormat(pattern = "yyyy-MM-dd")<br>
    private String fromDate;
    
    0 讨论(0)
  • 2020-12-17 19:07

    Taking a look at the comment with the error log it seems to be a conversion problem between String to java.util.Date. After searching for a while in the Thymeleaf GitHub I saw two issues which can explain how to proceed in this case:

    • Discussion of the conversion including date in this issue.
    • Implementation of the conversion is explained here.

    From the last point, I added an annotation to the start date of your project class:

    // This is "org.springframework.format.annotation.DateTimeFormat"
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date start;
    

    And after that, I was able to receive the date in your controller POST method.

    Take into account you also need to change your th:value and th:field attributes from your template for the date value from ${project.start} to *{start}, as I wrote in the comments, as you did for the name and description fields.

    0 讨论(0)
提交回复
热议问题