类之间的关系是1:N。在 Thymeleaf 中,我创建了一个拆分表单对象来提交表单数据。但是当我按下发送时,它会发送,但我在屏幕上看不到任何数据。
教授班
@Entity
@Getter
@Setter
@Table(name = "professor")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id", "username", "userid", "position", "sal", "hiredate"})
public class Professor {
@Id @GeneratedValue
@Column(name = "profno", nullable = false)
private Long id;
@Column(name = "name", nullable = false, length = 50)
private String username;
@Column(nullable = false, length = 50)
private String userid;
@Column(nullable = false, length = 20)
private String position;
private int sal;
private String hiredate;
private int comm;
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "deptno")
private Department department;
@OneToMany(mappedBy = "professor")
private List<Student> students = new ArrayList<>();
public Professor(String username) {
this.username = username;
}
public Professor(String username, String position) {
this.username = username;
this.position = position;
}
public Professor(String username, String position, int sal, String hiredate, int comm, String userid, Department department) {
this.username = username;
this.hiredate = hiredate;
this.userid = userid;
this.sal = sal;
this.comm = comm;
this.position = position;
if (department != null) {
setDepartment(department);
}
}
// ===== Method ===== //
public void setDepartment(Department department) {
this.department = department;
department.getProfessors().add(this);
}
部门.班级
@Entity
@Getter
@Setter
@Table(name = "department")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id", "name", "loc"})
public class Department {
@Id @GeneratedValue
@Column(name = "deptno", nullable = false)
private Long id;
@Column(name = "dname", length = 100)
private String name;
@Column(length = 100)
private String loc;
@OneToMany(mappedBy = "department")
private List<Professor> professors = new ArrayList<>();
@OneToMany(mappedBy = "department")
private List<Student> students = new ArrayList<>();
public Department(String name) {
this.name = name;
}
public Department(String name, String loc) {
this.name = name;
this.loc = loc;
}
}
教授表格.class
@Getter
@Setter
public class ProfessorForm {
private Long id;
@NotEmpty()
private String username;
private String userid;
private String position;
private String hiredate;
private int sal;
private int comm;
private Department department;
}
控制器类
Professor professor = new Professor(form.getUsername(), form.getUserid(),
form.getComm(), form.getHiredate(), form.getSal(), form.getPosition(), form.getDepartment());
百里香叶
<form th:action="@{/professor/add}" th:object="${professorForm}" method="post">
<div class="form-group">
<label th:for="username">name</label>
<input type="text" th:field="*{username}" class="form-control" placeholder="">
</div>
<div class="form-group">
<label th:for="userid">id</label>
<input type="text" th:field="*{userid}" class="form-control" placeholder="">
</div>
<div class="form-group">
<label th:for="position">position</label>
<input type="text" th:field="*{position}" class="form-control" placeholder="">
</div>
<div class="form-group">
<label th:for="sal">sal</label>
<input type="number" th:field="*{sal}" class="form-control" placeholder="">
</div>
<div class="form-group">
<label th:for="hiredate">hiredate</label>
<input type="date" th:field="*{hiredate}" class="form-control" placeholder="">
</div>
<div class="form-group">
<label th:for="comm">comm</label>
<input type="range" th:field="*{comm}" class="form-control" min="0" max="100" value="0" placeholder="">
</div>
<div class="form-group">
<label th:for="departmentId">loc</label>
<input type="text" th:field="*{department.loc}" class="form-control" placeholder="">
</div>
最后确认是department.loc问题。我们应该如何处理这个问题?
我假设您希望 HTML 表单的用户为要创建的教授实体选择某个部门。支持这一点的最简单方法是添加一个 HTML 选择,显示所有可能的部门,允许用户选择一个。
要使用 Thymeleaf 实现这一点,您需要以下内容:
ProfessorForm
以使用应引用的实体的主键(Department
在本例中,因此使用long
):public class ProfessorForm {
// instead of: private Department department;
private long departmentId;
}
@GetMapping
控制器方法中,返回所有部门名称和 ID。这需要填写 HTML 选择List<DepartmentNameAndId> departments = ...
model.addAttribute( "departments", departments);
<select th:field="*{departmentId}">
<option th:each="department : ${departments}"
th:text="${department.name}"
th:value="${department.id}">
</select>
@PostMapping
控制器的de中,使用departmentId
来获取相应的Department
对象并使用它来创建Professor
对象。有关更多详细信息,请参阅在 Thymeleaf 中使用 HTML 选择选项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句