簡體   English   中英

填表后空數據表thymeleaf和spring-boot

[英]Empty data table thymeleaf and spring-boot after filling 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>

桌子:

<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-agendas-html 由他自己渲染

填寫表格

但是 lista-agendas.html 點擊表單按鈕后,顯示沒有數據的表格:

單擊表單按鈕提交后表格中沒有數據

解決了 ! 剛剛在@PostMapping 守衛方法中替換

return "lista-agendas";

和:

return "redirect:/lista-agendas";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM