1.部门管理和岗位管理的功能实现是差不多的,首先看下都有哪些功能如图:(这里需要注意的比如如何显示上级部门?在添加的时候如何以树的结构来显示?等等需要非常注意的,本人在这其中遇到很多很多的错误,修改找错大改半天的时间,真的是看起来容易,动起手来都是错!!)
2.分析完功能,首先要做的就是几个请求?需要几个页面?需要几个方法?知道了以后先写DepartmentAction
解释:其中list方法是显示数据库中的相关数据,包括上级部门,上级部门是通过parent.name属性获取的,等下在jsp页面可以看到
因为在修改和添加页面中都需要用到select标签,其中的name属性为parentId,而我们的modle中是没有的,所以必须在action中手动编写此属性,并提供getter和setter方法。
还需要注意的就是回显的问题,方法都差不多,只是有的属性model中没有的我们需要另外获取
package com.icss.oa.view.action;
import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.icss.oa.base.BaseAction;
import com.icss.oa.domain.Department;
import com.opensymphony.xwork2.ActionContext;
@Controller
@Scope("prototype")
public class DepartmentAction extends BaseAction<Department> {
private Long parentId;
/** 列表 */
public String list() throws Exception {
List<Department> departmentList = departmentService.findAll();
ActionContext.getContext().put("departmentList", departmentList);
return "list";
}
/** 删除 */
public String delete() throws Exception {
departmentService.delete(model.getId());
return "toList";
}
/** 添加页面 */
public String addUI() throws Exception {
List<Department> departmentList=departmentService.findAll();
ActionContext.getContext().put("departmentList",departmentList );
return "addUI";
}
/** 添加 */
public String add() throws Exception {
// 1,新建对象并封装属性,也可以使用model
//将上级部门值保存
model.setParent(departmentService.getById(parentId));
// 2,保存到数据库中
departmentService.save(model);
return "toList";
}
/** 修改页面 */
public String editUI() throws Exception {
List<Department> departmentList=departmentService.findAll();
ActionContext.getContext().put("departmentList",departmentList );
// 准备回