gpt4 book ai didi

java - Spring MVC 将嵌套自定义类型列表绑定(bind)到多个 JSP 表单

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:52 26 4
gpt4 key购买 nike

案例:我有一个组织对象。它有一个部门对象列表,每个部门都有一个员工对象列表。

在 JSP 中,我有一个复选框列表,它将一个复选框绑定(bind)到一个员工对象(深入 2 个层次结构。即组织 -> 部门 -> 员工)。

<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

装订很好。提交表单后,在 Controller 中,我可以遍历 admin departmentList 并找到创建的所有部门,并找到由于复选框选择而创建的员工。

问题 :( 部门是用空名称和非空 employeeList 创建的。我找不到 employeeList 所属部门的名称 :( 那么我如何传递一些部门名称所以名称被注入(inject)部门(在创建时)就像“.employeeList”被注入(inject)部门一样。

详细信息:向您提供我工作的详细信息:

组织类有两个部门列表。Department 类有一个 Employees 列表。员工有姓名和 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。

我正在编写一个客户端,使用户能够创建“自定义组织”,方法是首先选择他们感兴趣的部门,然后对于所选的每个部门,用户从与该部门相关的所有员工中选择一个员工子集。

所以我有 3 个 JSP 表单:

组织表单 (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.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.jsp):对于在部门表单中选择的每个员工,这会显示员工姓名和用于输入将分配给该员工的工时的字段。

员工 Controller (部门名称为空的那个)

@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";
}

已知的:我明白为什么 department.name 没有被注入(inject),但我不确定如何解决它。如何将部门名称从外部循环注入(inject)到在下面的 c:foreach 中遍历员工列表的内部循环?或者如何以某种方式将它绑定(bind)到每个员工。我怀疑它在这里:

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>

我尝试了隐藏输入来帮助创建命名部门,但它没有帮助。

致谢:

非常感谢您的合作:)

为简单起见,假设员工的名字是唯一的。而 department.name 也是唯一的。谢谢。

最佳答案

新闻:哇哦,天哪,我解决了它。

缺失的部分:隐藏输入,告诉要创建哪个部门以及要注入(inject)什么名称。

<form:hidden path="adminDepartmentList[${statusDepartment.index}].name"  value="${department.name}" />

将部门名称注入(inject)由 spring 创建的每个部门。

整个解决方案:部门.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>

关于java - Spring MVC 将嵌套自定义类型列表绑定(bind)到多个 JSP 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19854171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com