Java Spring MVC 将嵌套的自定义类型列表绑定到多个 JSP 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19854171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC binding a List of nested Custom types to multiple JSP forms
提问by Rose
The Case:I've an Organization Object. It has a list of Department Objects, and each Department has a list of Employee Objects.
案例:我有一个组织对象。它有一个部门对象列表,每个部门都有一个员工对象列表。
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).
在 JSP 中,我有一个复选框列表,它将一个复选框绑定到一个员工对象(深入到 2 个层次结构。即组织->部门->员工)。
<input type="checkbox" name="adminDepartmentList[${status.index}].employeeList" value="${employee.firstName}"> <c:out value="${employee.firstName}" /><br>
As you can see:
如你看到的:
adminDepartmentList[0].employeeList --> John
adminDepartmentList[2].employeeList --> Rose
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.
绑定很好。提交表单后,在控制器中,我可以遍历管理部门列表并查找所有创建的部门并查找由于复选框选择而创建的员工。
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.
问题:(部门是用空名称和非空的employeeList创建的。我找不到employeeList所属部门的名称:(那么我如何传递一些部门名称以便将名称注入部门(如它正在创建)就像“.employeeList”如何被注入部门一样。
The Details:To give you the details of my work:
细节:给你我工作的细节:
An Organization class has two lists of Departments. A Department class has a list of Employees. Employee has first and last name and hourToWork.
一个组织类有两个部门列表。Department 类有一个员工列表。员工有名字和姓氏以及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
}
The list of Departments comes from an API. And all Employees of that department comes from another API.
部门列表来自 API。该部门的所有员工都来自另一个 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.
我正在编写一个客户端,它使用户能够通过首先选择他们感兴趣的部门,然后为每个选定的部门创建“自定义组织”,用户从与该部门相关的所有员工中选择一部分员工。
So I have 3 JSP forms:
所以我有 3 个 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.
组织表单(organization.jsp):组织名称的输入字段和所有部门的复选框列表。用户可以为正在创建的新组织选择一组部门。
<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 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.
部门表单(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="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 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.
员工表单 (employee.jsp):对于在部门表单中选择的每个员工,这将显示员工姓名和用于输入将分配给该员工的小时数的字段。
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";
}
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:
已知:我理解为什么没有注入 Department.name,但我不确定如何解决它。如何将部门名称从外循环注入到在下面 c:foreach 中运行员工列表的内循环?或者如何以某种方式将其绑定到每个员工。我怀疑它在这里:
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.
我尝试使用隐藏输入来帮助创建命名部门,但没有帮助。
The Credits:
学分:
Thanks so much for ur cooperation :)
非常感谢您的合作:)
For simplicity, assume FirstName for employee is unique. And department.name is also unique. Thanks.
为简单起见,假设员工的名字是唯一的。而且部门名称也是唯一的。谢谢。
采纳答案by Rose
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.
将部门名称注入 spring 创建的每个部门。
The Whole Solution:department.jsp
整体解决方案: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>