Issue
I'm having troubles with thymeleaf and spring boot to display data in table after a form filled succesfuly. Here is what i done:
Form:
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Cargar Agenda</h3>
</div>
<div th:if="${error != null}" th:text="${error}" class="alert alert-danger" role="alert">
</div>
<div class="panel-body">
<form th:action="@{/guardar}" method="POST">
<div class="form-group">
<input type="text" class="form-control" name="nombre" id="nombre" th:value="${nombre}" placeholder="Nombre">
</div>
<div class="form-group">
<input type="text" class="form-control" name="apellido" id="apellido" th:value="${apellido}" placeholder="Apellido">
</div>
<div class="form-group">
<input type="text" class="form-control" name="telefono" id="telefono" th:value="${telefono}" placeholder="Teléfono">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" id="email" th:value="${email}" placeholder="E-mail">
</div>
<div class="form-group">
<input type="text" class="form-control" name="domicilio" id="domicilio" th:value="${domicilio}" placeholder="Domicilio">
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">Guardar</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Table:
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover table-sm">
<thead class="thead-dark">
<tr class="bg-primary" scope="row">
<th scope="col">Nombre</th>
<th scope="col">Apellido</th>
<th scope="col">Teléfono</th>
<th scope="col">E-mail</th>
<th scope="col">Domicilio</th>
</tr>
</thead>
<tbody>
<tr scope="row" th:each="agenda : ${listaAgendas}">
<td scope="col" th:text="${agenda.nombre}"></td>
<td scope="col" th:text="${agenda.apellido}"></td>
<td scope="col" th:text="${agenda.telefono}"></td>
<td scope="col" th:text="${agenda.email}"></td>
<td scope="col" th:text="${agenda.domicilio}"></td>
</tr>
</tbody>
</table>
</div>
Controller PostMapping:
@PostMapping("/guardar")
public String guardar(ModelMap map, @RequestParam String nombre, @RequestParam String apellido,
@RequestParam Long telefono, @RequestParam String email,
@RequestParam String domicilio) {
try {
agendaService.crear(nombre, apellido, telefono, email, domicilio);
} catch (AgendaException ex) {
map.put("error", ex.getMessage());
map.put("nombre", nombre);
map.put("apellido", apellido);
map.put("telefono", telefono);
map.put("email", email);
map.put("domicilio", domicilio);
return "carga";
}
return "lista-agendas";
}
Controller GetMapping:
@GetMapping("/lista-agendas")
public String listaAgendas(ModelMap map) {
List<Agenda> listaAgendas = agendaService.listaAgendas();
map.put("listaAgendas", listaAgendas);
return "lista-agendas";
}
lista-agendas-html render this by his self
but lista-agendas.html after clicking the form button, display table without data:
no data in the table after clicking form button to submit
Solution
Solved ! just replaced in @PostMapping guardar method
return "lista-agendas";
with:
return "redirect:/lista-agendas";
Answered By - sixGdie