前端代码:

<a th:href="@{/delete/{id}(id=${data.id})}" onclick="return confirm('确定要删除吗?')">删除</a>

后端代码:

//在Controller中定义接口
@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id) {
    //调用Service层的删除方法
    userService.delete(id);
    //重定向到列表页面
    return "redirect:/list";
}

//在Service层中定义删除方法
public void delete(Long id) {
    userRepository.deleteById(id);
}

其中,userRepository为JPA的接口,继承了CrudRepository,因此可以直接调用其提供的删除方法。在Controller中,@PathVariable("id")注解表示从路径中获取id参数。在前端代码中,使用Thymeleaf的@{}语法构造删除链接,同时使用confirm()函数弹出确认框以防止误删

通过前端传过来的id删除数据库中存储的这条数据前端使用Thymeleaf编写后端使用springboot + jap编写

原文地址: https://www.cveoy.top/t/topic/cq9J 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录