6

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

在 JSP 中,我有一个复选框列表,它将复选框绑定到员工对象(深入 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

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

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

细节: 给你我工作的细节:

一个组织类有两个部门列表。一个部门类有一个员工列表。员工有名字和姓氏以及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):对于在部门表单中选择的每个员工,这将显示员工姓名和用于输入将分配给该员工的小时数的字段。

员工控制器(获得空部门名称的那个)

@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,但我不知道如何解决它。如何将部门名称从外部循环注入到运行在下面 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>

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

学分:

非常感谢您的合作:)

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

4

1 回答 1

5

新闻:哇哦,Holy Moly Cow,我解决了。

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

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

将部门名称注入 spring 创建的每个部门。

整体解决方案: 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>
于 2013-11-10T23:20:43.517 回答