Issue
I encountered problems trying to pass data from this template:
<form action="#" th:action="@{/pacientes/fechas}" th:object="${fechaInicioFinal}" method="GET">
<table>
<tr>
<td>
<label>Fecha Inicio :</label>
</td>
<td>
<input type="datetime-local" th:field="*{fechaInicio}">
</td>
</tr>
<tr>
<td>
<label>Fecha final :</label>
</td>
<td>
<input type="datetime-local" th:field="*{fechaFinal}">
</td>
</tr>
</table>
<input type="submit" value="Buscar">
</form>
To this controller, where my FechaInicioFinal only receive null values:
@GetMapping("/fechas")
public String buscarFechas(@ModelAttribute FechaInicioFinal fechaInicioFinal, BindingResult result, Model model,
RedirectAttributes redirectAttrs) {
LOG.info(fechaInicioFinal.toString()); // Here I get null values
// Business logic
}
This is the Controller where I use the template:
@GetMapping("/buscarFechas")
public String obtenerFechas( Model model, FechaInicioFinal fechaInicioFinal) {
model.addAttribute("fechaInicioFinal", fechaInicioFinal);
return "p_buscar_fechas";
}
I tried already these pages:
- https://bushansirgur.in/spring-mvc-pass-data-from-controller-to-view/
- How to pass data to from view to controller in Spring-MVC?
- Pass data from Thymeleaf template to springboot controller
- Using thymeleaf to post form data to a Controller that uses @ModelAttribute (complex objects)
Solution
Nevermind, there was a problem in the FechaInicioFinal class, was without this anotation
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime fechaInicio;
Answered By - Paulo Aldaz
Answer Checked By - Robin (JavaFixing Admin)