Spring整合Thymeleaf实现表单CRUD外键关联问题咨询
适配Thymeleaf表单提交的完整解决方案
核心逻辑说明
@Controller + Thymeleaf 做服务端渲染的场景下,表单默认提交格式为application/x-www-form-urlencoded,不需要使用@RequestBody接收JSON数据,也不用依赖@RestController的相关实现,直接用Spring自带的模型绑定即可完成表单数据接收、外键注入和入库操作,完全适配普通用户浏览器端操作的需求。
分步实现方案
- 第一步:配置实体类的外键关联
示例代码如下:@Entity public class Dish { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // 菜品名称 private String dishName; // 菜品价格 private BigDecimal price; // 是否无麸质 private Boolean glutenFree; // 外键关联所属餐厅 @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "restaurant_id", nullable = false) private Restaurant restaurant; // 省略Getter、Setter方法 } - 第二步:登录阶段存储所属餐厅ID
商家账号登录验证通过后,将账号关联的餐厅ID存入会话Session,避免用户手动提交外键,防止恶意篡改数据:// 商家登录验证逻辑执行完成后添加 session.setAttribute("currentRestaurantId", loginUser.getRestaurant().getId()); - 第三步:编写Thymeleaf提交表单
表单仅保留用户需要填写的字段,无需显式传递餐厅ID:<form th:action="@{/dish/save}" th:object="${dish}" method="post"> <div> <label>菜品名称:</label> <input type="text" th:field="*{dishName}" required> </div> <div> <label>菜品价格:</label> <input type="number" step="0.01" th:field="*{price}" required> </div> <div> <label>是否无麸质:</label> <input type="checkbox" th:field="*{glutenFree}"> </div> <button type="submit">保存菜品</button> </form> - 第四步:编写Controller层接口
提供表单跳转接口和提交处理接口,使用@ModelAttribute绑定表单数据,手动注入外键后保存:@Controller @RequestMapping("/dish") public class DishController { @Autowired private DishRepository dishRepository; @Autowired private RestaurantRepository restaurantRepository; // 跳转菜品新增页面 @GetMapping("/add") public String showAddPage(Model model) { model.addAttribute("dish", new Dish()); return "dish-add"; // 对应Thymeleaf模板文件名 } // 处理菜品提交请求 @PostMapping("/save") public String saveDish(@ModelAttribute Dish dish, HttpSession session) { // 从会话中获取当前登录商家的餐厅ID,注入为外键 Long restaurantId = (Long) session.getAttribute("currentRestaurantId"); Restaurant restaurant = restaurantRepository.findById(restaurantId).orElseThrow(); dish.setRestaurant(restaurant); // 直接保存入库,外键自动关联 dishRepository.save(dish); return "redirect:/dish/my-list"; // 保存成功跳转至商家菜品列表页 } }
常见问题排查
- 此前使用
@RequestParam报错,大概率是参数名称不匹配、数据类型转换失败导致的,使用@ModelAttribute绑定实体对象会自动处理类型转换和参数匹配,还支持JSR303参数校验,开发效率远高于逐个声明@RequestParam - 无需更换Thymeleaf模板引擎,上述实现是Spring Boot服务端渲染场景的标准开发方案,完全满足普通用户的操作需求,不需要适配前后端分离的JSON交互逻辑
内容的提问来源于stack exchange,提问作者Bronwyn Young
相关产品推荐
相关产品推荐

