SSH 在页面实现部门树结构

一、如图:
这里写图片描述

二、创建部门类:

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lovoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值