我有两个有很多关系的班级(员工和项目)。我正在尝试使用(项目)作为类对象和包含员工列表的下拉列表发送百万美元表单的内容。我可以在表单中检索员工列表,但是当我将其提交给控制器进行保存时,列表中的员工不会被保存。每次提交表格时,我都应该能够保存一名员工。
这是员工类:
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@Column(name = "EMPLOYEE_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "DOB")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dob;
@Column(name = "HIRING_DATE")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date hiringDate;
@Column(name = "TERMINATION_DATE")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date terminationDate;
@Column(name = "TELEPHONE")
private String telephone;
@Column(name = "EMAIL")
private String email;
@Column(name = "JOB_TITLE")
private String jobTitle;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "MANAGER_ID")
private Employee manager;
@OneToOne(cascade=CascadeType.ALL)
private EmployeeInfo empInfo;
@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinTable(name = "EMPLOYEE_PROJECT", joinColumns = { @JoinColumn(name = "EMPLOYEE_ID") }, inverseJoinColumns = {
@JoinColumn(name = "PROJECT_ID") })
Set<Project> projects;
public Employee() {
super();
}
public Employee(Long id, String firstName, String lastName, String telephone, String email, String jobTitle) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.telephone = telephone;
this.email = email;
this.jobTitle = jobTitle;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public Date getHiringDate() {
return hiringDate;
}
public void setHiringDate(Date hiringDate) {
this.hiringDate = hiringDate;
}
public Date getTerminationDate() {
return terminationDate;
}
public void setTerminationDate(Date terminationDate) {
this.terminationDate = terminationDate;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public Set<Project> getProjects() {
return projects;
}
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
public Employee getManager() {
return manager;
}
public void setManager(Employee manager) {
this.manager = manager;
}
public EmployeeInfo getEmpInfo() {
return empInfo;
}
public void setEmpInfo(EmployeeInfo empInfo) {
this.empInfo = empInfo;
}
}
这是Project类:
@Entity
@Table(name = "PROJECT")
public class Project {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "PROJECT_TYPE")
private String projectType;
@Column(name = "PROJECT_NAME")
private String name;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "START_DATE")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date startDate;
@Column(name = "END_DATE")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date endDate;
@Column(name = "COST")
private Float cost;
@ManyToMany(mappedBy = "projects", fetch = FetchType.EAGER)
private Set<Employee> employees;
public Project() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getProjectType() {
return projectType;
}
public void setProjectType(String projectType) {
this.projectType = projectType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public Float getCost() {
return cost;
}
public void setCost(Float cost) {
this.cost = cost;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
}
这是主要项目页面:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Projects List</h2>
<a href="#" th:href="@{/projects/new}" class="btn btn-primary"
role="button">Add New Project</a> <br> <br>
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Cost</th>
<th>Start Date</th>
<th>End Date</th>
<th>Project Type</th>
<th></th>
<th></th>
</tr>
<tr th:each="project : ${projects}">
<td th:text="${project.name}"></td>
<td th:text="${project.cost}"></td>
<td th:text="${project.startDate}"></td>
<td th:text="${project.endDate}"></td>
<td th:text="${project.projectType}"></td>
<td><a th:href="@{'/project/' + ${project.id} + '/update'}">Update</a></td>
<td><a th:href="@{'/project/' + ${project.id} + '/addTeam'}">Add Team</a></td>
</tr>
</table>
</div>
</body>
</html>
这是百里香的观点:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<body>
<div class="container">
<h1>
<span th:text="${project.name }" th:remove="tag">title</span>
</h1>
<h3>
<span th:text="${project.id }" th:remove="tag">title</span>
</h3>
<br>
<h2>Add Project Team</h2>
<br>
<form class="form-group" th:object="${project}"
th:action="@{/projects/saveProject}" method="post">
<input th:type="hidden" name="id" th:field="${project.id}" />
<div class="form-group">
<label for="employees">Add Team:</label>
<select name="employees"
id="employees">
<option th:each="data:${employeesList}" th:value="${data.id}"
th:text="${data.firstName}" >Options</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Save</button>
<a href="#" th:href="@{/projects}" class="btn btn-danger"
role="button">Cancel</a>
</form>
</div>
</body>
</html>
这是控制器:
@RequestMapping("project/{id}/addTeam")
public String addProjectTeam(@PathVariable String id, Model model) {
model.addAttribute("project", projectService.findById(Long.valueOf(id)));
model.addAttribute("employeesList", this.employeeService.getEmployees());
return "addTeam";
}
@PostMapping("/projects/saveProject")
public String saveOrUpdate(Model model, @RequestParam("project") Project project,
@RequestParam("employees") Set<Long> employeesIds)
{
Set<Employee> employees = new LinkedHashSet<Employee>();
if (project.getEmployees() != null && project.getEmployees().size() > 0) {
for (Employee emp : project.getEmployees()) {
employeesIds.add(emp.getId());
}
for (Long eId : employeesIds) {
Employee e = this.employeeService.findById(eId);
employees.add(e);
}
project.setEmployees(employees);
this.projectService.saveProject(project);
}
model.addAttribute("projects", projectService.getProjects());
return "redirect:/projects/";enter image description here
}
感谢您帮助解决问题。
答案 0 :(得分:0)
显然你想发送&#39;员工&#39;从HTML-Drop-down到你的控制器的对象,你无法获得。!! 如果是这样的情况,根据您的代码 - 您还没有设置&#39;员工&#39;你的百万富翁代码中的对象,我也想告诉你,你不能从你的&#39;员工那里得到所有的价值,比如dob,job_title,地址。下拉列,因为HTML下拉列表仅包含名称及其值。像这样的东西
col3
要解决此问题,请发送您的&#39; Employee-Id&#39; &安培; &#39;雇员名&#39;从控制器到百万美元,然后在获得“员工-ID”之后再进一步控制器上的对象,执行“选择查询”#39;从DB获取员工的详细信息。 为此:---
1。)在您发送员工和管理员的控制器上项目的对象使用模型属性,如下所示: -
WithColumn
2。)(现在这部分很重要)获取Thyemleaf代码上的员工ID和员工姓名以及项目:---
<select name="Employee">
<option value="Id">David</option>
<option value="Id">Peter</option>
</select>
3。)获取所选Employee_id&amp;的列表。你的控制器上的Project_details如下: -
@RequestMapping("/getProjectAndEmployee")
public String getProjectAndEmployee(Model model) {
List<YourEmployeeEntityClass> employees = //your code to get the employee
List<YourProjectEntityClass> project = //your code to get project
model.addAttribute("employees",employees);
model.addAttribute("project",project);
}