我在这里做错了什么? 我收到这个错误...
无法将java.lang.String类型的属性值转换为属性部门所需的com.akybenko.entity.Department类型;嵌套异常是java.lang.IllegalStateException:无法将java.lang.String类型的值转换为必需类型com.akybenko.entity.Department for property department:找不到匹配的编辑器或转换策略
请帮我解决这个问题。
EmployeeController.java
package com.akybenko.controller;
import java.beans.PropertyEditorSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.akybenko.entity.Department;
import com.akybenko.entity.Employee;
import com.akybenko.service.DepartmentService;
import com.akybenko.service.EmployeeService;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@Autowired
private DepartmentService departmentService;
class EmployeeEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
int id = Integer.parseInt(text);
Employee employee = employeeService.get(id);
setValue(employee);
}
}
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Employee.class, new EmployeeEditor());
}
@GetMapping("/list")
public String listEmployees(Model model) {
List<Employee> employees = employeeService.getAll();
model.addAttribute("employees", employees);
return "list-employees";
}
@GetMapping("/add")
public String getAddEmployee(Model model) {
model.addAttribute("addEmployee", new Employee());
model.addAttribute("listDept", departmentService.getAll());
return "addEmployee";
}
@PostMapping("/add")
public String add(
@ModelAttribute("addEmployee") Employee employee,
BindingResult result,
Model model) {
if (result.hasErrors()) {
return "addEmployee";
}
employeeService.add(employee);
return "redirect:/employee/list";
}
}
addEmployee.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ page import="com.akybenko.entity.Department" %>
<!DOCTYPE html>
<html>
<head>
<title>Add Employee</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/addEmployee.css" />
</head>
<body>
<div id = "wrapper">
<div id = "header">
<h2>ADD EMPLOYEE</h2>
</div>
</div>
<div id = "container">
<form:form action="add" modelAttribute="addEmployee" method="post">
<div class="row">
<label for="employeeId">ID</label>
<div>
<form:input path="employeeId"/>
</div>
</div>
<div class="row">
<div>
<label for="employeeLastName">Last Name</label>
<div>
<form:input path="employeeLastName"/>
</div>
</div>
</div>
<div class="row">
<div>
<label for="employeeFirstName">First Name</label>
<div>
<form:input path="employeeFirstName"/>
</div>
</div>
</div>
<div class="row">
<div>
<label for="department">Department</label>
<div>
<form:select path="department">
<form:option value="NONE">---SELECT---</form:option>
<form:options items="${listDept}" itemLabel="departmentName" itemValue="departmentId" />
</form:select>
</div>
<div>
<form:errors path="department" />
</div>
</div>
</div>
<div><input type="submit" value="Save" id="buttonSave"/> </div>
<div><input type="reset" value="Reset" id="buttonReset"/></div>
</form:form>
</div>
</body>
</html>
Employee.java
package com.akybenko.entity;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMP_SEQUENCE")
@SequenceGenerator(name="EMP_SEQUENCE", sequenceName="employee_seq", allocationSize=1)
@Column(name="employee_id")
private int employeeId;
@Column(name="employee_lname")
private String employeeLastName;
@Column(name="employee_fname")
private String employeeFirstName;
@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="department_id", referencedColumnName="department_id", nullable = false, insertable = false, updatable = false)
private Department department;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeLastName() {
return employeeLastName;
}
public void setEmployeeLastName(String employeeLastName) {
this.employeeLastName = employeeLastName;
}
public String getEmployeeFirstName() {
return employeeFirstName;
}
public void setEmployeeFirstName(String employeeFirstName) {
this.employeeFirstName = employeeFirstName;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", employeeLastName=" + employeeLastName + ", employeeFirstName="
+ employeeFirstName + ", department=" + department + "]";
}
}
Department.java
package com.akybenko.entity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="department")
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DEPT_SEQUENCE")
@SequenceGenerator(name="DEPT_SEQUENCE", sequenceName="department_seq", allocationSize=1)
@Column(name="department_id")
private int departmentId;
@Column(name="department_name")
private String departmentName;
@OneToMany(mappedBy="department", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<Employee> employees;
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
@Override
public String toString() {
return "Department [departmentId=" + departmentId + ", departmentName=" + departmentName + ", employees="
+ employees + "]";
}
}
答案 0 :(得分:0)
我在这里做错了什么?
Department
是参考表。最好从中移除employees
。
请添加课程DepartmentDto
,EmployeeDto
并在网络上使用它们。
在EmployeeService#add()
方法中:
departmentId
获取EmployeeDto
。departmentId
部门。EmployeeDto
转换为Employee
并将Department
设置为Employee
(您可以使用load()
方法,或者创建一个假的部门仅departmentId
。