您的位置:首页 > 其它

SSH 在页面实现部门树结构

2016-10-04 15:39 204 查看
一、如图:



二、创建部门类:

public class Department {
private Long id;
private Set<User> users = new HashSet<User>();
private Department parent;
private Set<Department> children = new HashSet<Department>();

private String name;
private String description;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Set<User> getUsers() {
return users;
}

public void setUsers(Set<User> users) {
this.users = users;
}

public Department getParent() {
return parent;
}

public void setParent(Department parent) {
this.parent = parent;
}

public Set<Department> getChildren() {
return children;
}

public void setChildren(Set<Department> children) {
this.children = children;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}


三、创建部门action:

@Controller
@Scope("prototype")
public class DepartmentAction extends BaseAction<Department> {
private static final long serialVersionUID = 5242842204530086588L;

private Long parentId;

public String list() throws Exception {
//List<Department> departmentList = departmentService.findAll();
List<Department> departmentList = null;
if (parentId == null) {
departmentList = departmentService.findTopList();
}else {
departmentList = departmentService.findChildList(parentId);
//返回上一级所使用的
Department parent = departmentService.getById(parentId);
ActionContext.getContext().put("parent", parent);
}
ActionContext.getContext().put("departmentList", departmentList);
return "list";
}

//// 准备数据:departmentList,显示为树状结构
public String addUI() throws Exception {
// 回显数据
List<Department> departmentList = DepartmentUtil.getAllDepartments(departmentService.findTopList());
ActionContext.getContext().put("departmentList", departmentList);
return "saveUI";
}

public String add() throws Exception {
if (parentId != null) {
Department parent = departmentService.getById(parentId);
model.setParent(parent);
}
departmentService.add(model);
return "toList";
}

public String delete() throws Exception {
departmentService.delete(model.getId());
return "toList";
}

public String editUI() throws Exception {
// 回显数据
//List<Department> departmentList = departmentService.findAll();
List<Department> departmentList = DepartmentUtil.getAllDepartments(departmentService.findTopList());
ActionContext.getContext().put("departmentList", departmentList);

Department department = departmentService.getById(model.getId());
ActionContext.getContext().getValueStack().push(department);

//回显上级部门
if (department.getParent() != null) {
parentId = department.getParent().getId();
}

return "saveUI";
}

public String edit() throws Exception {
// 1.获得实体
Department department = departmentService.getById(model.getId());
// 2.设值
department.setName(model.getName());
department.setDescription(model.getDescription());

//3.设置上级部门
Department parent = departmentService.getById(parentId);
department.setParent(parent);

// 4.更新
departmentService.update(department);
return "toList";
}

@Override
public Department getModel() {
return model;
}

public void setDepartment(Department department) {
this.model = department;
}

public Long getParentId() {
return parentId;
}

public void setParentId(Long parentId) {
this.parentId = parentId;
}

}


四、创建部门树工具类:

public class DepartmentUtil {

public static List<Department> getAllDepartments(List<Department> topList){
List<Department> list = new ArrayList<Department>();
walkDepartmentTreeList(topList, " ├", list);
return list;
}

private static void walkDepartmentTreeList(Collection<Department> topList, String prifix, List<Department> list) {
for(Department department : topList){
Department copy = new Department();
if(department.getParent() == null)
copy.setName(department.getName());
else
copy.setName(prifix + department.getName());
list.add(copy);
walkDepartmentTreeList(department.getChildren(), " " + prifix, list);
}
}
}


五、前端页面:

<table cellpadding="0" cellspacing="0" class="mainForm">
<tr><td width="100">上级部门</td>
<td><s:select list="#departmentList" cssClass="SelectStyle" name="parentId" listKey="id" listValue="name" headerKey="" headerValue="=请选择上级部门="></s:select>
</td>
</tr>
<tr><td>部门名称</td>
<td><s:textfield name="name" cssClass="InputStyle required" />  *</td>
</tr>
<tr><td>职能说明</td>
<td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td>
</tr>
</table>


六、strust2配置:

<action name="*_*" class="{1}Action" method="{2}">
<result name="list">/WEB-INF/jsp/{1}/list.jsp</result>
<result name="saveUI">/WEB-INF/jsp/{1}/saveUI.jsp</result>
<result name="toList" type="redirectAction">{1}_list?parentId=${parentId}</result>
</action>


七、BaseAction代码:

@SuppressWarnings("all")
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

protected T model;

@Resource
protected DepartmentService departmentService;
@Resource
protected RoleService roleService;

public BaseAction(){
ParameterizedType pt =  (ParameterizedType) this.getClass().getGenericSuperclass();
Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
try {
model = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

@Override
public T getModel() {
return model;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ssh 结构