Reputation: 2083
i have class X with field:
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
private Date updateDate;
I have html with method="post"
<form class="form-horizontal"
th:action="@{/x/save}" method="post">
I want to get current date and sent it via POST to updateDate field:
<span th:text="${#dates.format(#dates.createNow(), 'dd/MM/yyyy HH:mm')}"
th:value="${#dates.format(#dates.createNow(), 'dd/MM/yyyy HH:mm')}"
th:id="*{updateDate}" th:name="*{updateDate}">
</span>
the problem is that date is not sent via POST to the updateDate
field. I checked it in browser via Developer Tools
.
I can not use only th:field
here cause i want current date and i acquire it via:
th:value="${#dates.format(#dates.createNow(), 'dd/MM/yyyy HH:mm')}"
like this it is also not sent via POST:
<input th:text="${#dates.format(#dates.createNow(), 'dd/MM/yyyy HH:mm')}"
th:value="${#dates.format(#dates.createNow(), 'dd/MM/yyyy HH:mm')}"
th:id="${updateDate}" th:name="${updateDate}"/>
But I see correct date in html.
Solution:
<input name="updateDate" id="updateDate"
th:value="${#dates.format(#dates.createNow() , 'dd/MM/yyyy HH:mm')}"/>
Beware:
this is not perfect solution:
Upvotes: 2
Views: 3322
Reputation: 5753
If you dont want to use Spring for binding to your input just use plain HTML id and name attributes. Also you cannot use a span element to post data. You have to use an input tag. eg <input name="updateDate" id="updateDate" th:value="${#dates.format(#dates.createNow() , 'dd/MMM/yyyy HH:mm')}"/>
A better approach is to set your updateDate value in the controller and using Spring data binding syntax.
<input type="text" th:field="*{updateDate}"/>
Upvotes: 4