@PostMapping("/addAddress")
public String showFormForAddAddress(@RequestParam("studentId") int id, Model model) {
Student student = studentService.findStudentById(id);
Address address = new Address();
model.addAttribute("address", address);
List<Address> addresses = student.getAddresses();
addresses.add(address);
student.setAddresses(addresses);
studentService.saveStudent(student);
return "address-form";
}
@GetMapping("/addAddress")
public String addAddress(@RequestParam("studentId") int id, Model model) {
Address address = new Address();
address.setStudentId(id);
model.addAttribute("address", address);
return "address-form";
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Address Form</title>
</head>
<body>
<div id="wrapper">
<div id="header">
<h2>SRM Student Relationship Manager (Address)</h2>
</div>
</div>
<div id="container">
<h3>Save Address</h3>
<form:form action="addAddress" modelAttribute="address" method="POST">
<form:input path="studentId" />
<table>
<tbody>
<tr>
<td><label>Address One</label></td>
<td><form:input path="addressOne" /></td>
</tr>
<tr>
<td><label>City</label></td>
<td><form:input path="city" /></td>
</tr>
<tr>
<td><label>State</label></td>
<td><form:input path="state" /></td>
</tr>
<tr>
<td><label>Zip Code</label></td>
<td><form:input path="zipCode" /></td>
</tr>
<tr>
<td><label>Address Two</label></td>
<td><form:input path="addressTwo" /></td>
</tr>
<tr>
<td><label></label></td>
<td><input type="submit" value="Save"></td>
</tr>
</tbody>
</table>
</form:form>
</div>
</body>
</html>
@Entity
@Table(name = "address", catalog = "user_db")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "address_id")
private Integer id;
private String addressOne;
private String city;
private String state;
private String zipCode;
private String addressTwo;
@Column(name = "user_id")
private Integer studentId;
@Entity
@Table(name = "Student", catalog = "user_db")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "student_id")
private int id;
private String firstName;
private String lastName;
private String email;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "student_id", referencedColumnName = "student_id")
private List<Address> addresses;
我觉得这可能是一个命名问题,但我不能完全指责它,我也尝试删除帖子,只是使用get,仍然没有工作。
这应该是showForForAddMethod
@PostMapping("/addAddress")
public String showFormForAddAddress(@RequestParam("studentId") int id, Model model,
@ModelAttribute("address") Address address) {
Student student = studentService.findStudentById(id);
model.addAttribute("address", address);
List<Address> addresses = student.getAddresses();
addresses.add(address);
student.setAddresses(addresses);
studentService.saveStudent(student);
return "redirect:/student/list";
}
如果我创建一个新对象,则信息不会被映射,因此它会丢失,但是当我将地址作为参数中的模型属性传递时,这指向了学生对象
在你的showFormForAddAddress
方法中,你编写了model.addAttribute("address", address);
,它将值放入模型属性中。
但是你试图从这种方法中获取地址信息,这是错误的。
您可以使用以下方法获取相关信息:
Map<String,Object> modelMap = model.getModel().asMap();
Object object = modelMap.get("address");
之后,您可以将结果对象转换为地址类型并使用它