填写表格后清空数据表thymelaf和spring-boot



我遇到了thymelaf和spring-boot的问题,无法在成功填写表单后在表中显示数据。以下是我所做的:

形式:

<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>

表:

<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>

控制器后映射:

@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";
}

控制器获取映射:

@GetMapping("/lista-agendas")
public String listaAgendas(ModelMap map) {
List<Agenda> listaAgendas = agendaService.listaAgendas();
map.put("listaAgendas", listaAgendas);
return "lista-agendas";
}

lista议程html通过自己的呈现

填写表单

但是lista-agendas.html点击表单按钮后,显示没有数据的表格:

点击表单按钮提交后,表中没有数据

已解决!刚刚在@PostMapping瓜达尔方法中替换

return "lista-agendas";

带有:

return "redirect:/lista-agendas";

最新更新