我使用Spring MVC模式和Hibernate编写第一个表单。
我想更新表中的现有行时遇到问题。
单击表中的“编辑”按钮后,在db中找到正确的行。然后,我可以更新“名称”字段并单击“保存”按钮。在数据库中,我创建了具有新ID的新行。
我写了两个System.out
来检查对象的当前值。
我的控制器类:
@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
类不包含任何注释。我不知道这是否是一个错误,但是该对象是控制器和服务层之间的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>
<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>
什么让我好奇。从数据库中检索对象时具有比创建模型并发送到
edit_intervention.html
表单适当的属性(id,名称)。更改名称后,它返回到保存方法,但是ID号为“ null”,而不是包含更改后的新值的名称。对于我(初学者)而言,似乎没有所有属性都从视图传输到控制器中的save方法。当我们将ID设置为“ null”时,这表示休眠创建新行。
我猜有一个小故障,我找不到。
当我调用更新表单时,从终端记录日志:
并查看修改后的表的外观:
预先感谢您的帮助。
最佳答案
如您所见,您的id字段为空。因为可能没有设置Long值。
在实体类中,应将id属性标记为@Id
,也可以始终在上面添加@GeneratedValue
批注。