转:传智播客—jbpm与OA项目(五)续一

6.删除部门

/**

 * 删除部门

 */

public ActionForward del(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    // 获取部门id并删除

    String id = request.getParameter("id");

    this.deptServ.delete(Long.parseLong(id));

    // 因为删除部门后我们需要显示与被添加部门的所有同级部门,所以在传递给显示页面上级部门的ID

    ActionForward af = mapping.findForward("toList");

    return new ActionForward(af.getPath() + "&parentId="

            + request.getParameter("parentId"), af.getRedirect());

}

 

         上面这些方法十分简单!

 

二、员工管理

员工管理的难点在于,员工具有一个部门和多个岗位的特性。我们通过指定员工对象的部门属性和员工集合属性,可以简单的描述它们之间的关系。但添加或修改员工时,员工的部门和岗位的属性如何显示在页面上呢?我们的页面表单使用的是strutshtml标签,部门使用下拉列表框显示,岗位使用列表框显示。这两个组件的选中项,只需要对应的id值即可。

 

这样,我们就需要将员工的部门属性和岗位集合属于转换为对应的id即可。我们将EmployeeActionForm对部门属性定义为“Long departmentId;”,岗位集合属性定义为“Set<Role> roleIdList;”,具体看下面的代码。

 

1.员工列表

/**

 * 员工列表

 */

public ActionForward list(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    List<Employee> employees = this.empServ.findAll();

    request.setAttribute("employeeList", employees);

    return mapping.findForward("list"); // list.jsp

}

 

         2.跳转到添加员工页面

/**

 * 添加员工页面

 */

public ActionForward addUI(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    // 获取部门和岗位列表,提供给添加员工时使用

    List<Department> depts =  this.deptServ.findAll();

    List<Role> roles = this.roleServ.findAll();

    request.setAttribute("departmentList", DepartmentUtils.getAllDepartmentList(depts));

    request.setAttribute("roleList", roles);

    return mapping.findForward("saveUI"); // saveUI.jsp

}

 

         3.添加员工

/**

 * 添加员工

 */

public ActionForward add(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    // 获取页面提交过来的员工信息

    EmployeeActionForm empForm = (EmployeeActionForm) form;

    Employee emp = new Employee();

    BeanUtils.copyProperties(emp, empForm);

    // 根据选中的部门id,获取部门对象

    emp.setDepartment(this.deptServ.getById(empForm.getDepartmentId()));

    // 根据选中的岗位id数组,获取岗位对象集合

    Set<Role> roles = new HashSet(this.roleServ.findByQuery(empForm.getRoleIdList()));       

    emp.setRoles(roles);

    // 保存

    this.empServ.save(emp);

    // 跳转到显示员工列表页面

    return mapping.findForward("toList");

}

 

4.跳转到修改员工页面

/**

 * 修改员工页面

 */

public ActionForward editUI(ActionMapping mapping, ActionForm form,

        HttpServletRequest request, HttpServletResponse response)

        throws Exception {

    // 获取被修改员工的信息

    Long id = Long.parseLong(request.getParameter("id"));

    Employee emp = this.empServ.getById(id);

    // 将被修改员工的信息封装到ActionForm

    EmployeeActionForm empForm = (EmployeeActionForm) form;

    BeanUtils.copyProperties(empForm, emp);

    // 获取部门ID

    empForm.setDepartmentId(emp.getDepartment().getId());

    // 根据岗位集合获取对象岗位id数组

    empForm.setRoleIdList(this.roleServ.getIdsByRoles(emp.getRoles()));

    // 获取部门和岗位列表,并添加到request的属性中

    List<Department> depts =  this.deptServ.findAll();

    List<Role> roles = this.roleServ.findAll();

    request.setAttribute("departmentList", DepartmentUtils.getAllDepartmentList(depts));

    request.setAttribute("roleList", roles);

    // 跳转到修改页面    

    return mapping.findForward("saveUI"); // saveUI.jsp

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值