gpt4 book ai didi

java - Spring MVC 在数据库中创建新行,同时想要更新

转载 作者:行者123 更新时间:2023-12-01 22:54:01 25 4
gpt4 key购买 nike

我使用 Spring MVC 模式和 Hibernate 编写了我的第一个表单。
当我想更新表中的现有行时遇到问题。在表中单击“编辑”Buttno 后,在数据库中找到正确的行。然后我可以更新“名称”字段并单击“保存”按钮。比在数据库中我用新 ID 创建了新行。

我编写了两个System.out来检查对象的当前值。

我的 Controller 类:

@Controller
//@RequestMapping("/")
public class InterventionController {

@Autowired
private InterventionService interventionService;

@GetMapping("/")
public String viewHomePage(Model model){
List<InterventionModel> interventionList = interventionService.listAll();
model.addAttribute("interventionList", interventionList);
return "index";
}

@GetMapping("/new")
public String addInterventionForm(Model model){
InterventionModel interventionModel = new InterventionModel();
model.addAttribute("interventionModel", interventionModel);
System.out.println(model.toString());
return "new_intervention";
}

@RequestMapping(value="/save", method = RequestMethod.POST)
public String saveIntervention(@ModelAttribute("interventionModel")InterventionModel interventionModel){
System.out.println("Object retreived from editing view "+interventionModel); // <- just to check in console object attributes
interventionService.save(interventionModel);
return "redirect:/";
}

@RequestMapping("/edit/{id}")
public ModelAndView showEditInterventionForm(@PathVariable(name="id") Long id){
ModelAndView mav = new ModelAndView("edit_intervention");
InterventionModel interventionModel = interventionService.get(id).get(); // <-- Optional received from service layer
mav.addObject("interventionModel", interventionModel);
System.out.println("Object retrieved from database : "+interventionModel); // <- just to check in console object attributes
return mav;
}

@GetMapping("/delete/{id}")
public String deleteIntervention(@PathVariable(name="id") Long id){
interventionService.delete(id);
return "redirect:/";
}

我的 InterventionModel 类不包含任何注释。我不知道这是否是一个错误,但该对象是 Controller 和服务层之间的 DTO。在服务层,它被映射到被注释为@Entity的实体。

public class InterventionModel {

private Long id;
private String name;

public InterventionModel() {
}

public InterventionModel(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "InterventionModel{" +
"id=" + id +
", name='" + name + '\'' +
'}';
} }

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Interventions</title>
</head>
<body>

<div align="center">
<h1>Interventions</h1>
<a href="new">Create new Intervention</a>
<br><br>
<table border="2" cellpadding="10">
<thead>
<tr>
<th>Intervention Id</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="intervention :${interventionList}">
<td th:text="${intervention.id}">Intervention Id</td>
<td th:text="${intervention.name}">Name</td>
<td>
<a th:href="@{'/edit/' + ${intervention.id}}">Edit</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a th:href="@{'/delete/' + ${intervention.id}}">Delete</a>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

edit_intervention.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Edit Intervention</title>
</head>
<body>
<div align="center">
<h1>Edit Intervention</h1>
<br/>
<form action="#" th:action="@{/save}" th:object="${interventionModel}" method="post">
<table border="0" cellpadding="10">
<tr>
<td>Intervention ID</td>
<!-- <td><input type="text" th:field="*{id}" readonly="readonly"/></td>-->
<td><input type="text" th:field="*{id}" readonly="readonly"/></td>
</tr>
<tr>
<td>Intervention name</td>
<td><input type="text" th:field="*{name}"/></td>
</tr>
<tr>
<td colspan="2"><button type="submit">Save</button> </td>
</tr>
</table>
</form>
</div>

</body>
</html>

我好奇什么。从数据库检索的对象具有正确的属性(id、名称),而模型被创建并发送到 edit_intervention.html 表单。更改名称后,返回到保存方法,但 ID 号为“null”,而不是包含更改后的新值的名称。
对于我(作为初学者)来说,似乎没有所有属性都从 View 转移到 Controller 中的保存方法。当我们将 ID 设置为“null”时,这是 hibernate 创建新行的标志。
我想有一个我找不到的小故障。
当我调用更新表单时从终端记录:
enter image description here
并查看修改后的表格:
enter image description here

提前感谢您的帮助。

最佳答案

如您所见,您的 id 字段为空。因为可能没有设置 Long 值。在实体类中,您应该将 id 属性标记为 @Id,也可以随时在上面添加 @GenerateValue 注解。

关于java - Spring MVC 在数据库中创建新行,同时想要更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58447965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com