如果Employee中有另一个对象,如何在JSP中正确保存Employee

时间:2015-02-26 19:39:10

标签: java spring hibernate jsp spring-mvc

员工实体:

@Entity
@Table(name = "EMPLOYEE")
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne(targetEntity = Department.class)
@JoinColumn(name = "department_id")
private Department department;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "SALARY")
private int salary;

EmployeeDao有两个可变路径来保存员工:

@Override
@Transactional(readOnly = false)
public void addEmployee(int departmentId, String firstName, String lastName, int salary) {
    Department department = getDepartmentById(departmentId);
    Employee employee = new Employee(department, firstName, lastName, salary);
    sessionFactory.getCurrentSession().save(employee);
}

@Override
@Transactional(readOnly = false)
public void addEmployee(Employee employee) {
    sessionFactory.getCurrentSession().save(employee);
}

...

MainController:

@RequestMapping(value = "/employee/add", method = RequestMethod.GET)
public ModelAndView addPage(Model model) {
    model.addAttribute("emp", new Employee());
    List<Department> departments = dao.getAllDepartments();
    model.addAttribute("deps", departments);
    return new ModelAndView("Add");
}

@RequestMapping(value = "/employee/add", method = RequestMethod.POST)
public ModelAndView addEmployee(@ModelAttribute("emp") Employee employee) {

    System.out.println(employee);
    //dao.addEmployee(employee);
    return new ModelAndView("redirect:/");
}

和addPage.jsp

<c:url var="saveUrl" value="/employee/add" />

<form:form modelAttribute="emp" method="POST" action="${saveUrl}" >
    <p>Department : <select><c:forEach var="dep" items="${deps}">

            <option name="department" value="${dep.id}"> ${dep.name}</option>

    </c:forEach></select>
    </p>
    <p>
        First Name : <input size="40px" type="text" name="firstName" />
    </p>
    <p>
        Last Name : <input size="40px" type="text" name="lastName" />
    </p>
    <p>
        Salary : <input type="text" name="salary" />
    </p>
    <input height="300px" type="submit" value="Add User" />
</form:form>

如何从JSP中将Department设置为我的Employee?

另外:我有一个正常运行的REST-POST方法,但我不知道如何在JSP中使用它。

@RequestMapping(value = "/add/employee/{departmentId}/{firstName}/{lastName}/{salary}", method = RequestMethod.POST, headers = "Accept=application/json")
public void addEmployee(@PathVariable int departmentId,@PathVariable String firstName,@PathVariable String lastName,@PathVariable int salary) {
    dao.addEmployee(departmentId, firstName, lastName, salary);
    dao.calculateAvgSalaryInDepartment();
}

帮助,请

1 个答案:

答案 0 :(得分:0)

您可以直接传递具有相应键和保存实体的对象,而不是传递子ID。

即。用这个

替换你的选择标签
<select>
    <c:forEach var="dep" items="${deps}">
       <option name="department" value="${dep}"> ${dep.name}</option>
    </c:forEach>
</select>

如果你只想传递孩子的主键,然后使用@InitBinder和spring Validator来识别孩子。

参考this example

注意: 在休息方法中,您通过按departmentId部门添加员工以便它正常工作,并且在您的jsp帖子请求中,您正在传递员工实体,只是部门ID而不是部门对象