首注:本学习教程为传智播客汤阳光讲师所公布的免费OA项目视频我的文字版实践笔记,本人用此来加强巩固自己开发知识,如有网友转载,请注明。谢谢。
一 功能分析:
1,列表页面只显示一层的(同级的)部门数据,默认显示最顶级的部门列表。
2,点击部门名称,可以查看此部门相应的下级部门列表。
3,删除部门时,同时删除此部门的所有下级部门。
4,上级部门的列表是有层次结构的(树形)。
5,如果是修改:上级部门列表中不能显示当前修改的部门及其子孙部门。因为不能选择自已或自已的子部门作为上级部门。
二 首先,将前台页面作修改,内容如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags"%> 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title>部门列表</title> 8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 9 <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script> 10 <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script> 11 <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script> 12 <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" /> 13 <script type="text/javascript"> 14 </script> 15 </head> 16 <body> 17 18 <div id="Title_bar"> 19 <div id="Title_bar_Head"> 20 <div id="Title_Head"></div> 21 <div id="Title"><!--页面标题--> 22 <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 部门管理 23 </div> 24 <div id="Title_End"></div> 25 </div> 26 </div> 27 28 <div id="MainArea"> 29 <table cellspacing="0" cellpadding="0" class="TableStyle"> 30 31 <!-- 表头--> 32 <thead> 33 <tr align=center valign=middle id=TableTitle> 34 <td width="150px">部门名称</td> 35 <td width="150px">上级部门名称</td> 36 <td width="200px">职能说明</td> 37 <td>相关操作</td> 38 </tr> 39 </thead> 40 41 <!--显示数据列表--> 42 <tbody id="TableData" class="dataContainer" > 43 <s:iterator value="#departmentList"> 44 <tr class="TableDetail1 template"> 45 <td><s:a action="department_list?parentId=%{id}">${name}</s:a> </td> 46 <td>${parent.name} </td> 47 <td>${description} </td> 48 <td><s:a onClick="return window.confirm(‘这将删除所有的下级部门,您确定要删除吗?‘)" action="department_delete?id=%{id}">删除</s:a> 49 <s:a action="department_editUI?id=%{id}">修改</s:a> 50 </td> 51 </tr> 52 </s:iterator> 53 </tbody> 54 </table> 55 56 <!-- 其他功能超链接 --> 57 <div id="TableTail"> 58 <div id="TableTail_inside"> 59 <s:a action="department_addUI"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></s:a> 60 </div> 61 </div> 62 </div> 63 64 <!--说明--> 65 <div id="Description"> 66 说明:<br /> 67 1,列表页面只显示一层的(同级的)部门数据,默认显示最顶级的部门列表。<br /> 68 2,点击部门名称,可以查看此部门相应的下级部门列表。<br /> 69 3,删除部门时,同时删除此部门的所有下级部门。 70 </div> 71 72 </body> 73 </html>
list.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags"%> 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 6 <html> 7 <head> 8 <title>部门设置</title> 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 10 <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script> 11 <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script> 12 <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script> 13 <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" /> 14 </head> 15 <body> 16 17 <!-- 标题显示 --> 18 <div id="Title_bar"> 19 <div id="Title_bar_Head"> 20 <div id="Title_Head"></div> 21 <div id="Title"><!--页面标题--> 22 <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 部门信息 23 </div> 24 <div id="Title_End"></div> 25 </div> 26 </div> 27 28 <!--显示表单内容--> 29 <div id=MainArea> 30 <s:form action="department_%{id == null ? ‘add‘:‘edit‘}"> 31 <div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1"> 32 <IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 部门信息 </DIV> --> 33 </div> 34 <s:hidden name="id"></s:hidden> 35 <!-- 表单内容显示 --> 36 <div class="ItemBlockBorder"> 37 <div class="ItemBlock"> 38 <table cellpadding="0" cellspacing="0" class="mainForm"> 39 <tr><td width="100">上级部门</td> 40 <td> 41 <s:select name="parentId" list="#departmentList" 42 cssClass="SelectStyle" listKey="id" listValue="name" headerKey="" headerValue="==请选择部门=="/> 43 </td> 44 </tr> 45 <tr><td>部门名称</td> 46 <td><s:textfield name="name" cssClass="InputStyle"/> *</td> 47 </tr> 48 <tr><td>职能说明</td> 49 <td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td> 50 </tr> 51 </table> 52 </div> 53 </div> 54 55 <!-- 表单操作 --> 56 <div id="InputDetailBar"> 57 <input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/> 58 <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a> 59 </div> 60 </s:form> 61 </div> 62 63 <div class="Description"> 64 说明:<br /> 65 1,上级部门的列表是有层次结构的(树形)。<br/> 66 2,如果是修改:上级部门列表中不能显示当前修改的部门及其子孙部门。因为不能选择自已或自已的子部门作为上级部门。<br /> 67 </div> 68 69 </body> 70 </html>
saveUI.jsp
三 修改action对应的方法,修改后action内容如下:
1 package cn.clear.oa.view.action; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.springframework.context.annotation.Scope; 8 import org.springframework.stereotype.Controller; 9 10 import cn.clear.oa.domain.Department; 11 import cn.clear.oa.service.DepartmentService; 12 13 import com.opensymphony.xwork2.ActionContext; 14 import com.opensymphony.xwork2.ActionSupport; 15 import com.opensymphony.xwork2.ModelDriven; 16 17 @Controller 18 @Scope("prototype") 19 public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{ 20 21 /** 22 * 23 */ 24 private static final long serialVersionUID = 1L; 25 @Resource 26 private DepartmentService departmentService; 27 private Department model = new Department(); 28 private Long parentId; 29 30 public Department getModel() { 31 // TODO Auto-generated method stub 32 return model; 33 } 34 35 36 public String list() throws Exception { 37 List<Department> departmentList = null; 38 if(parentId == null){ 39 departmentList = departmentService.findTopList(); 40 }else{ 41 departmentList = departmentService.findChildren(parentId); 42 } 43 ActionContext.getContext().put("departmentList", departmentList); 44 return "list"; 45 } 46 public String delete() throws Exception { 47 48 departmentService.delete(model.getId()); 49 50 return "toList"; 51 } 52 public String add() throws Exception { 53 //封装信息到对象中 54 Department parent = departmentService.findById(parentId); 55 model.setParent(parent); 56 departmentService.save(model); 57 return "toList"; 58 } 59 public String addUI() throws Exception { 60 //准备departmentList数据 61 List<Department> departmentList = departmentService.findAll(); 62 //放在值栈中的map中 63 ActionContext.getContext().put("departmentList", departmentList); 64 return "saveUI"; 65 } 66 public String edit() throws Exception { 67 68 Department department = departmentService.findById(model.getId()); 69 department.setName(model.getName()); 70 department.setDescription(model.getDescription()); 71 department.setParent(departmentService.findById(parentId));//设置所属的上级部门 72 departmentService.update(department); 73 return "toList"; 74 } 75 public String editUI() throws Exception { 76 //准备departmentList数据 77 List<Department> departmentList = departmentService.findAll(); 78 //放在值栈中的map中 79 ActionContext.getContext().put("departmentList", departmentList); 80 81 Department department = departmentService.findById(model.getId()); 82 //将对象放在栈顶 83 ActionContext.getContext().getValueStack().push(department); 84 //回显上级部门 85 if(department.getParent()!=null){ 86 parentId = department.getParent().getId(); 87 } 88 return "saveUI"; 89 } 90 91 //------ 92 public Long getParentId() { 93 return parentId; 94 } 95 96 97 public void setParentId(Long parentId) { 98 this.parentId = parentId; 99 } 100 101 }
DepartmentAction.java
四 在显示顶级部门和下级部门时需要增加service方法。直接在service方法中引入操作数据库的代码,所以service层内容修改如下(对于分层结构的调整,后面还要进行修改):
1 package cn.clear.oa.service; 2 3 import java.util.List; 4 5 import cn.clear.oa.domain.Department; 6 7 public interface DepartmentService { 8 9 List<Department> findAll(); 10 11 void delete(Long id); 12 13 void save(Department department); 14 15 Department findById(Long id); 16 17 void update(Department department); 18 /** 19 * 查询顶级部门列表 20 * @return 21 */ 22 List<Department> findTopList(); 23 /** 24 * 查询子部门列表 25 * @return 26 */ 27 28 List<Department> findChildren(Long parentId); 29 30 31 }
DepartmentService.java
1 package cn.clear.oa.service.impl; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.hibernate.SessionFactory; 8 import org.springframework.stereotype.Service; 9 import org.springframework.transaction.annotation.Transactional; 10 11 import cn.clear.oa.dao.DepartmentDao; 12 import cn.clear.oa.domain.Department; 13 import cn.clear.oa.service.DepartmentService; 14 @Service 15 @Transactional 16 @SuppressWarnings("unchecked") 17 public class DepartmentServiceImpl implements DepartmentService{ 18 19 @Resource 20 private DepartmentDao departmentDao; 21 22 @Resource 23 private SessionFactory sessionFactory; 24 25 26 public List<Department> findAll() { 27 28 return departmentDao.findAll(); 29 } 30 31 public void delete(Long id) { 32 33 departmentDao.delete(id); 34 35 } 36 37 public void save(Department department) { 38 39 departmentDao.save(department); 40 } 41 42 public Department findById(Long id) { 43 44 return departmentDao.findById(id); 45 } 46 47 public void update(Department department) { 48 49 departmentDao.update(department); 50 } 51 52 public List<Department> findTopList() { 53 // TODO Auto-generated method stub 54 return sessionFactory.getCurrentSession().createQuery(// 55 "FROM Department d WHERE d.parent IS NULL")// 56 .list(); 57 } 58 59 public List<Department> findChildren(Long parentId) { 60 // TODO Auto-generated method stub 61 return sessionFactory.getCurrentSession().createQuery(// 62 "FROM Department d WHERE d.parent.id = ?")// 63 .setParameter(0, parentId) 64 .list(); 65 } 66 67 }
DepartmentServiceImpl.java
五 因为删除上级部门要连同下级部门一并删除,所以要使用到级联操作;而我们service层直接使用session会导致懒加载的问题,所以要修改下映射文件内容,如下:
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="cn.clear.oa.domain"> 7 <class name="Department" table="oa_department"> 8 <id name="id"><generator class="native"/></id> 9 <property name="name"/> 10 <property name="description"/> 11 <!-- users属性,本类与User的1对多 --> 12 <set name="users"> 13 <key column="departmentId"></key> 14 <one-to-many class="User"/> 15 </set> 16 <!-- parent属性,本类与(上级)Department的多对1 --> 17 <many-to-one name="parent" class="Department" column="parentId" lazy="false"></many-to-one> 18 <!-- children属性,本类与(下级)Department的1对多 --> 19 <set name="children" cascade="delete" lazy="false"> 20 <key column="parentId"></key> 21 <one-to-many class="Department"/> 22 </set> 23 </class> 24 </hibernate-mapping>
Department.hbm.xml
所有一切都修改完毕之后,启动服务器,测试即可。
时间: 2024-10-24 19:59:28