使用 thymeleaf + jpa 从数据库编辑行

Edit row from database using thymeleaf + jpa

我正在尝试将 table 中显示的数据作为输入,以便我可以随意编辑。这就是我在 thymeleaf 中使用 if-s 的原因。因此,当我按下编辑按钮时,id_modific 成为我要更改的行的 ID(我之前从 html->this works 中获取了它)。 所以现在,什么也没有发生,当我按下编辑图像时,什么也没有发生。 :( 我该怎么做才能让它发挥作用?或者我怎样才能使用 thymeleaf + jpa 做我想做的事?

提前谢谢你。你是最棒的。

部分控制器:

@GetMapping("c/edit/{id}")
public ModelAndView editClientData(@PathVariable(value="id") int id, Model model) {
        Client client = clientDao.findById(id);
        model.addAttribute("id_modific", id);
        return new ModelAndView("redirect:/adminhomepage");
    }

@GetMapping("/finish-edit-client")
public String FinishEditClient(@RequestParam("id_client") int id_client,
                                         @RequestParam("nume") String nume,
                                         @RequestParam("prenume") String prenume,
                                         @RequestParam("cnp") String cnp,
                                         @RequestParam("telefon") String telefon,
                                         @RequestParam("email") String email) {
        clientService.modifyClientById(id_client, nume, prenume, cnp, telefon, email);

部分html代码:

<tr th:each="c,iStat:${client}"
   th:style="${iStat.odd}? 'font-weight: bold;'"
   th:alt-title="${iStat.even}? 'even' : 'odd'"
   th:with="one=1">
<span th:if="${c.id_client} != ${id_modific}">
   <td style = "word-wrap: break-word;" th:text="${c.id_client}"></td>
   <td style = "word-wrap: break-word;" th:text="${c.nume}"></td>
   <td style = "word-wrap: break-word;" th:text="${c.prenume}"></td>
   <td style = "word-wrap: break-word;" th:text="${c.cnp}"></td>
   <td style = "word-wrap: break-word;" th:text="${c.telefon}"></td>
   <td style = "word-wrap: break-word;" th:text="${c.cont_online}"></td>
   <td style = "word-wrap: break-word;" th:text="${c.email}"></td>
   <td style = "word-wrap: break-word;" th:text="${c.parola}"></td>
   <td><a th:href="@{/c/delete/{id}(id=${c.id_client})}" class="btn btn-primary"><i class="fas fa-trash-alt"></i></a>
     <a th:href="@{/c/edit/{id}(id=${c.id_client})}" class="btn btn-primary"><i class="fas fa-edit"></i></a> </td>
</span>
<span th:if="${c.id_client} == ${id_modific}">
    <form action="finish-edit-client" method="get">
        <td name = "id_client" style = "word-wrap: break-word;" th:text="${c.id_client}"></td>
        <td><input name="nume" class="form-control"></td>
        <td><input name="prenume" class="form-control"></td>
        <td><input name="cnp" class="form-control"></td>
        <td><input name="telefon" class="form-control"></td>
        <td style = "word-wrap: break-word;" th:text="${c.cont_online}"></td>
        <td><input name="email" class="form-control"></td>
        <td style = "word-wrap: break-word;" th:text="${c.parola}"></td>
         <td><button type="submit" class="btn btn-primary"><i class="fas fa-check"></i></button></td>
</form>
</span>

How a row of the table looks like

您不能以这种方式将 <span> 放在 <tr> 中 - 这是无效的 HTML。

如果您使用 Thymeleaf th:if="${...}" 运算符在单元格组之间进行选择,请尝试使用 <th:block> 标签而不是 <span>。这就是所谓的 synthetic tag,它将被 Thymeleaf 删除。但是 Thymeleaf 会将内容保留在块内 - 即您选择的 <td> 标签及其数据。