Spring/Thymeleaf 初學者提前抱歉,但我有 2 個物體 Employee 和 MeetingInfo。Employee 與 MeetingInfo 具有 oneToMany 關系,因此基本上一個 Employee 可以有許多 MessageInfo。使用 psvm,我可以使用以下方法將具有多個 MessageInfo 的新員工添加到我的資料庫中:
Employee employee1 = new Employee("Employee 1");
MeetingInfo mInfo1 = new MeetingInfo(LocalDate.of(2021, 1, 1), "First Random message");
MeetingInfo mInfo2 = new MeetingInfo(LocalDate.of(2021, 2, 2), "Second Random message");
MeetingInfo mInfo3 = new MeetingInfo(LocalDate.of(2021, 3, 3), "Third Random message");
employee1.getMeetingInfo().add(mInfo1);
employee1.getMeetingInfo().add(mInfo2);
employee1.getMeetingInfo().add(mInfo3);
employeeRepository.save(employee1);
但是我怎么能用百里香中的表格做到這一點呢?我可以只添加一個員工,但不能添加一個新的 MeetingInfo 物件。當我這樣做時,我得到一個 passException 錯誤。
我的 new_employee.html
<form action="#" th:action="@{/ines/saveEmployee}" th:object="${employee}"
method="POST">
<input type="text" th:field="*{name}"
placeholder="Employee Name" class="form-control mb-4 col-4">
*** so if I remove between here***
<input type="date" th:field="*{meetingInfo.meetingDate}"
placeholder="Message Date" class="form-control mb-4 col-4">
<input type="text" th:field="*{meetingInfo.message}"
placeholder="Message" class="form-control mb-4 col-4">
*** and here***
*** how can I include a MessageInfo object with a new Employee?***
<button type="submit" class="btn btn-info col-2">Save Meeting</button>
</form>
我的控制器
@GetMapping("/showNewEmployeeForm")
public String showNewEmployeeForm(Model model) {
Employee employee = new Employee();
model.addAttribute("employee", employee);
return "meeting/new_employee.html";
}
@PostMapping("/saveEmployee")
public String saveEmployee(@ModelAttribute("employee") Employee employee) {
employeeService.saveMessage(employee);
return "redirect:/ines/employees";
}
員工
@Entity
@Table(name = "employee")
public class Employee {
@Id
@Column(name = "employee_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long employeeId;
@Column(nullable = false)
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "employee_id", referencedColumnName = "employee_id")
private List<MeetingInfo> meetingInfo = new ArrayList<>();
//Constructors, getters and setters
會議資訊
@Entity
@Table(name = "meeting_info")
public class MeetingInfo {
@Id
@Column(name = "meeting_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long meetingId;
private String message;
@Column(name = "meeting_date")
private LocalDate meetingDate;
//Constructors, getters and setters
uj5u.com熱心網友回復:
使用單個請求保存多個物體并不是您通常想要對 Spring Boot 應用程式執行的操作,但是,由于我知道這僅用于練習,您可以通過使用單個 DTO 物件來執行此操作,該物件將保存兩個物體的資訊:
public class EmployeeMeetingDTO {
private String employeeName;
private String meetingMessage;
private LocalDate meetingDate;
}
然后,您的控制器可以從請求中只接受一個 DTO 物體:
@PostMapping("/saveEmployee")
public String saveEmployee(@ModelAttribute("employeeDto") EmployeeMeetingDTO employeeDto) {
employeeService.saveMessage(employeeDto);
return "redirect:/ines/employees";
}
您可以在 EmployeeService 類中分別創建這兩個物體。您的 Thymeleaf 表單將如下所示:
<form action="#" th:action="@{/ines/saveEmployee}" th:object="${employeeDto}"
method="POST">
<input type="text" th:field="*{employeeName}"
placeholder="Employee Name" class="form-control mb-4 col-4">
<input type="date" th:field="*{meetingDate}"
placeholder="Message Date" class="form-control mb-4 col-4">
<input type="text" th:field="*{meetingMessage}"
placeholder="Message" class="form-control mb-4 col-4">
<button type="submit" class="btn btn-info col-2">Save Meeting</button>
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516952.html
上一篇:如何使用條件對表進行物件更新
