Spring MVC将嵌套的Custom类型列表绑定到多个JSP表单 [英] Spring MVC binding a List of nested Custom types to multiple JSP forms
问题描述
案例:
我是一个组织对象。它有一个Department Objects列表,每个Department都有一个Employee Objects列表。
The Case: I've an Organization Object. It has a list of Department Objects, and each Department has a list of Employee Objects.
在JSP中,我有一个复选框列表,它将一个复选框绑定到一个雇员对象(深层次的2个层次结构。即组织 - >部门 - >员工)。 / p>
In JSP, I have a checkbox list that binds a check box to an employee object (deep down 2 hierarchies. That is Organization->Department->Employee).
<input type="checkbox" name="adminDepartmentList[${status.index}].employeeList" value="${employee.firstName}"> <c:out value="${employee.firstName}" /><br>
如您所见:
adminDepartmentList[0].employeeList --> John
adminDepartmentList[2].employeeList --> Rose
绑定很好。提交表单后,在控制器中,我可以遍历admin departmentList并查找创建的所有部门,并找到由于复选框选择而创建的员工。
The binding is good. After form is submitted, in the controller, I can loop over admin departmentList and find all departments created and find the employees that were created due to the checkbox selection.
问题:(部门是使用空名称和非null employeeList创建的。我找不到employeeList所属的部门名称:(那么如何传递一些部门名称以便注入名称到部门(正在创建)就像.employeeList被注入部门一样。
The Issue :( The departments are created with null names and non-null employeeList. I cannot find the names of department to which the employeeList belongs :( So how can I pass some department-name so the name gets injected to the department (as it is being created) just like how the ".employeeList" is getting injected to the department.
详细信息:
为了向您提供我工作的详细信息:
The Details: To give you the details of my work:
组织类有两个部门列表。
部门类有一个Employees列表。
员工拥有姓名和hourToWork。
An Organization class has two lists of Departments. A Department class has a list of Employees. Employee has first and last name and hourToWork.
public class Organization{
private long id;
private String name;
private List<Department> adminDepartmentList; //n admin departments
private List<Department> employeeDepartmentList //m employee departments
// default constructor and all getters and setters
}
public class Department{
private long id;
private String name;
private List<Employee> employeeList; //k employees
//default constructor and all getters and setters
}
public class Employee{
private long id;
private String firstName;
private String lastName;
private int hoursToWork; // to be filled from Spring MVC form
//default contructor and all getters and setters
}
部门列表来自API。该部门的所有员工都来自另一个API。
The list of Departments comes from an API. And all Employees of that department comes from another API.
我正在编写一个客户端,通过首先选择他们感兴趣的部门来创建自定义组织然后,对于所选的每个部门,用户从与该部门相关的所有员工中选择一部分员工。
I am writing a client that enables users to create "customized organizations" by first selecting the departments they are interested in and then for each department that was selected, the user selects a subset of employees from all employees related to that department.
所以我有3个JSP表单:
So I have 3 JSP forms:
组织表单(organization.jsp):组织名称的输入字段和所有部门的复选框列表。用户可以为正在创建的新组织选择一组部门。
Organization form (organization.jsp): input field for the name of the organization and a check box list of all departments. User can select a set of Department for the new organization that's being created.
<form:form name='fs' action="department.htm" method='POST' commandName="organization">
Organization Name:
<input type="text" name="name" >
<!-- ============================================================== -->
Departments:<br> Select admin-departments you want.
<div class="checkbox-list">
<%-- Size :<c:out value="${organization.adminDepartmentList.size}"/> --%>
<c:forEach var="i" varStatus="status" items="${organization.adminDepartmentList}">
<input type="checkbox" name="adminDepartmentList" value="${i.name}"> <c:out value="${i.name}" /><br>
</c:forEach>
</div>
<!-- ============================================================== -->
Departments:<br> Select employee-departments you want.
<div class="checkbox-list">
<c:forEach var="i" varStatus="status" items="${organization.employeeDepartmentList}">
<input type="checkbox" name="employeeDepartmentList" value="${i.name}"> <c:out value="${i.name}" /><br>
</c:forEach>
</div>
<button type="submit" class="btn btn-lg btn-primary btn-block">Next Step</button>
</form:form>
部门表格(department.jsp):对于所选的每个部门,这都显示了一个检查要为部门选择的员工清单。
Department form (department.jsp): For each of the departments that were selected, this show a check box list of employees to be selected for the department.
<form:form name='f' action="employee.htm" method='POST' commandName="organization">
Organization Name: <c:out value="${organization.name}" /><br>
Select Employees you want for your new Departments.
Admin Departments:<br>
<c:forEach var="department" varStatus="status" items="${organization.adminDepartmentList}">
______Dept: <c:out value="${department.name}" /><br>
<div class="checkbox-list">
<c:forEach var="employee" varStatus="status" items="${department.employeeList}">
<input type="checkbox" name="adminDepartmentList[${status.index}].employeeList" value="${employee.firstName}"> <c:out value="${employee.firstName}" /><br>
</c:forEach>
</div>
</c:forEach>
Employee Departments:<br>
<c:forEach var="department" varStatus="status" items="${organization.employeeDepartmentList}">
______Dept: <c:out value="${department.name}" /><br>
<div class="checkbox-list">
<c:forEach var="employee" varStatus="status" items="${department.employeeList}">
<input type="checkbox" name="employeeDepartmentList[${status.index}].employeeList" value="${employee.firstName}"> <c:out value="${employee.firstName}" /><br>
</c:forEach>
</div>
</c:forEach>
<button type="submit" class="btn btn-lg btn-primary btn-block">Next Step</button>
</form:form>
员工表格(employee.jsp):对于在部门表格中选择的每个员工,显示员工姓名和输入将分配给该员工的小时数的字段。
Employee form (employee.jsp): For each of the employee that was selected in Department Form, this shows the employee name and a field to enter the hours that will be assigned to that employee.
员工控制器(获取空部门名称的那个)
The Employee Controller (the one that's getting null department-name)
@RequestMapping(value = "/employee", method = RequestMethod.POST)
public String product_post(@ModelAttribute("organization") Organization organization, HttpServletRequest request,
HttpServletResponse response, BindingResult result, ModelMap model) {
System.out.println("=========== POST Employee CONTROLLER===============");
//STEP 1. show me which employees are selected for each admin department.
List<Department> adminDepartments = organization.getAdminDepartmentList();
for(Department dept: adminDepartments){
System.out.println("Admin Dept name::: " + dept.getName()); //<-----------Name Comes as null :(
List<Employee> employeeList = dept.getEmployeeList();
for(Employee emp: employeeList){
System.out.println("Employee::"+ emp.getFirstName());
}
}
//STEP 2. show me which employees are selected for each employee department.
List<Department> employeeDepartments = organization.getEmployeeDepartmentList();
for(Department dept: employeeDepartments){
System.out.println("Employee Dept name::: " + dept.getName()); //<----------Name Comes as null :(
List<Employee> employeeList = dept.getEmployeeList();
for(Employee emp: employeeList){
System.out.println("Employee::"+ emp.getFirstName());
}
}
model.addAttribute("organization", organization);
return "employee";
}
已知:
我联合国derstand为什么没有注入department.name,但我不知道如何解决它。如何将外部循环中的部门名称注入到下面c:foreach中员工列表上运行的内部循环?或者如何以某种方式将其绑定到每个员工。我怀疑它在这里:
The Known: I understand why department.name is not being injected, but I'm not sure how to solve it. How do I inject the department name from the outer loop to the inner loop that runs over the employee list in the below c:foreach? Or how to somehow bind it to each employee. I'm suspecting it's here:
Admin Departments:<br>
<c:forEach var="department" varStatus="status" items="${organization.adminDepartmentList}">
______Dept: <c:out value="${department.name}" /><br>
<div class="checkbox-list">
<c:forEach var="employee" varStatus="status" items="${department.employeeList}">
<input type="checkbox" name="adminDepartmentList[${status.index}].employeeList" value="${employee.firstName}"> <c:out value="${employee.firstName}" /><br>
</c:forEach>
</div>
</c:forEach>
我尝试了隐藏的输入来帮助创建命名部门,但它没有帮助。
I tried hidden input to help create named departments but it didn't help.
积分:
为简单起见,假设员工的FirstName是唯一的。而department.name也是独一无二的。
谢谢。
For simplicity, assume FirstName for employee is unique. And department.name is also unique. Thanks.
推荐答案
新闻:哦哇,神圣的莫莉牛,我解决了它。
The News: Oh wow, Holy Moly Cow, I solved it.
缺失的部分:
隐藏的输入,告诉你要创建什么部门以及要注入的名称。
The Missing Part: hidden input that tells what department to create and what name to inject to it.
<form:hidden path="adminDepartmentList[${statusDepartment.index}].name" value="${department.name}" />
为春天创建的每个部门注入部门名称。
Injecting department name to each of the department that was being created by spring.
整体解决方案:
department.jsp
The Whole Solution: department.jsp
<form:form name='f' action="employee.htm" method='POST' commandName="organization">
Organization Name: <c:out value="${organization.name}" /><br>
Select Employees you want for your new Departments.
Admin Departments:<br>
<c:forEach var="department" varStatus="statusDepartment" items="${organization.adminDepartmentList}">
______Dept: <c:out value="${department.name}" /><br>
<div class="checkbox-list">
<form:hidden path="adminDepartmentList[${statusDepartment.index}].name" value="${department.name}" />
<c:forEach var="employee" varStatus="statusEmployee" items="${department.employeeList}">
<form:checkbox path="adminDepartmentList[${statusDepartment.index}].employeeList" value="${employee.firstName}"/> <c:out value="${employee.firstName}" /><br>
</c:forEach>
</div>
</c:forEach>
Employee Departments:<br>
<c:forEach var="department" varStatus="statusDepartment" items="${organization.employeeDepartmentList}">
______Dept: <c:out value="${department.name}" /><br>
<div class="checkbox-list">
<form:hidden path="employeeDepartmentList[${statusDepartment.index}].name" value="${department.name}" />
<c:forEach var="employee" varStatus="statusEmployee" items="${department.employeeList}">
<form:checkbox path="employeeDepartmentList[${statusDepartment.index}].employeeList" value="${employee.firstName}"/> <c:out value="${employee.firstName}" /><br>
</c:forEach>
</div>
</c:forEach>
<button type="submit" class="btn btn-lg btn-primary btn-block">Next Step</button>
</form:form>
这篇关于Spring MVC将嵌套的Custom类型列表绑定到多个JSP表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!