Thymeleaf does not display date in HTML5 component

Asked

Viewed 691 times

0

Hello! I’m starting to study Hymeleaf with Spring MVC and I came across a problem that I can’t solve.

I have a date field with the component type="date" HTML5 in a form, which works 100% when registering. However, when I use the form for editing, all form data is displayed in its HTML components, minus the date.

When parsing the page’s HTML source code the date is there, but does not appear in the visual component.

<input type="date" class="form-control" id="data" name="data" value="03/03/1985">

When I use JSP/JSTL whenever the form opens for editing the date in the HTML5 component already appears in the input, but it is not appearing with Thyemeleaf.

Does anyone know the reason and how to fix it? That’s my input:

<input th:field="*{data}" type="date" class="form-control" />

1 answer

0


I found the problem. I had created a Formatter class to format the date and it seems that was the problem. When creating this class I removed the annotation @DateTimeFormat of the attribute of LocalDate. I ended up removing the Formatter class and returning with the cited annotation. And to format the date on the page, I used the dependency

<dependency>
  <groupId>org.thymeleaf.extras</groupId>
  <artifactId>thymeleaf-extras-java8time</artifactId>
  <version>3.0.0.RELEASE</version>
</dependency>

I registered it in the Template Engine configuration

templateEngine.addDialect(new Java8TimeDialect());

and when displaying formatted date on the page

<td th:text="${#temporals.format(user.data, 'dd MMM yyyy')}"></td>

This way I could then format the date to display on the page and also the question of the date in the input date field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.