Home > OS >  Cannot pass data from view to controller spring thymeleaf
Cannot pass data from view to controller spring thymeleaf

Time:08-24

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:

CodePudding user response:

Nevermind, there was a problem in the FechaInicioFinal class, was without this anotation

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime fechaInicio;
  • Related