SSH 在页面实现部门树结构
2016-10-04 15:39
204 查看
一、如图:
二、创建部门类:
三、创建部门action:
四、创建部门树工具类:
五、前端页面:
六、strust2配置:
七、BaseAction代码:
二、创建部门类:
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; } }
相关文章推荐
- 页面中用实现的树形结构 《添加,删除,添加全部,删除全部》
- 在frame结构的页面中,如何实现用struts2跳转时,只改变页面中的某个frame
- 为Html页面设置背景——通过设置body结构标签和CSS指定背景属性实现
- JSP实现树型组织架构(5)--删除部门页面orgDelete.jsp
- SSH 实现页面跳转
- 一个拓扑结构题得实现(2011baidu校招研发部门的面试题目)
- [举重若轻]html5 history api实现单页面结构
- [置顶] SSH整合之四(页面逻辑的实现)
- 前端页面实现树形结构
- iframe结构的网站按F5刷新子页面的实现方式
- eShop电子商城的SSH实现的用户模块(1)登录和注册配置文件和页面关键代码(V...
- 在jsp页面中实现递归显示树状结构(树状bbs)
- 部门下拉框的树状结构展示小功能的实现
- Combotree 实现树结构-前台页面点击某个结点,input框为xx-xx-xx内容
- 东软JavaWeb实训记-DAY4-Servlet+frame框架页面的实现+不带框架的完整项目结构
- SSH与SSM学习之SSH实现CRM练习06——客户列表06_页面和测试
- JS构建页面的DOM节点结构的实现代码
- 任务七:实现常见的技术产品官网的页面结构及样式布局
- HTML页面树型结构的实现-xyTree V4.1
- 一个拓扑结构题得实现(2011baidu校招研发部门的面试题目)