You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot控制器问题:HTML表单编辑数据时新增而非更新

问题分析与修复方案

核心问题

  1. POST接口拿不到要修改的图书ID:你的@PostMapping("/edit")路径没带{id}变量,方法里的@PathVariable(value = "id") Integer id根本取不到值,等于白写,最后查询不到要修改的原图书。
  2. 表单没传图书ID:页面表单里没加图书ID的隐藏字段,提交后Spring MVC生成的Book对象ID是空的,JPA默认会把ID为空的实体当成新数据执行新增操作。
  3. GET接口冗余参数:bookListUpdate方法里的@Valid BookDto book完全多余,当前场景不需要提前绑定这个对象。

修复步骤

1. 修正控制器代码

// GET请求:加载编辑页面,删掉没用的BookDto参数
@GetMapping("/bookUpdate/{id}")
public String bookListUpdate(@PathVariable(value = "id") Integer id, Model model) {
    model.addAttribute("book", service.findById(id));
    return "views/bookUpdate";
}

// POST请求:用表单传ID的方案,不用改URL结构更灵活
@PostMapping("/edit")
public String editBook(@Valid Book book) {
    // 先查到数据库里的原图书
    Book existingBook = service.findById(book.getId());
    // 更新字段
    existingBook.setDescription(book.getDescription());
    existingBook.setTopic(book.getTopic());
    existingBook.setLink(book.getLink());
    // 保存更新
    service.saveBook(existingBook);
    // 重定向到首页,避免用户刷新重复提交表单
    return "redirect:/";
}

2. 修正Thymeleaf表单页面

必须加一个隐藏的ID字段,让表单提交时把图书ID传回去:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
layout:decorate="~{fragments/main_layout}">
<head>
<title>Book Form</title>
</head>
<body>
<div layout:fragment="content" class="container mySpace">

    <form th:action="@{/edit}" th:object="${book}" method="post">
        <!-- 新增隐藏ID字段,必须加! -->
        <input type="hidden" th:field="*{id}" />
        
        <div class="form-group">
            <label for="topic" class="form-control-label">Topic</label> 
            <input type="text" class="form-control" th:field="*{topic}" id="topic" />
        </div>

        <div class="form-group">
            <label for="description" class="form-control-label">Description</label>
            <textarea class="form-control" th:field="*{description}" id="description" style="height: 95px"></textarea>
        </div>

        <div class="form-group">
            <label for="link" class="form-control-label">Link</label>
            <input type="text" class="form-control" th:field="*{link}" id="link" />
        </div>

        <input type="submit" value="Submit" class="btn btn-primary" />
    </form>
</div>
</body>
</html>

额外提醒

  • 确保你的Book实体类ID字段配置了正确的JPA注解(比如@Id、@GeneratedValue),这样JPA才会识别ID存在时执行更新,否则还是会新增。
  • 用redirect:/代替直接返回页面是好习惯,能避免重复提交表单的问题。
  • 可以给editBook加个异常处理,比如查不到对应ID的图书时,跳回列表页提示用户。

内容的提问来源于stack exchange,提问作者Anastasia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 22:31:12