系統管理模塊_部門管理1_實現基本的增刪改查功能
先不考慮上級部門
設計實體、表
1、設計實體
Department.java
public class Department { private Long id; private String name; private String description; public Long getId() { return id; } public void setId(Long id) { this.id = id; } 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; } }
2、寫映射文件
Department.hbm.xml
<hibernate-mapping package="cn.itcast.oa.domain"> <class name="Department" table="itcast_department"> <id name="id"> <generator class="native" /> </id> <property name="name"></property> <property name="description"></property> </class> </hibernate-mapping>
3、把映射文件添加到hibernate.cfg.xml中
<mapping resource="cn/itcast/oa/domain/Department.hbm.xml" />
4、創建表
運行測試類SpringTest.java中的testSessionFactory()方法創建SessionFactory即可

分析功能與請求
增刪改查共4個功能,6個請求,需要在Action中有6個對應的處理方法。
| 作用 |
方法名 |
返回值 |
對應的JSP頁面 |
| 列表 |
list() |
list |
list.jsp |
| 刪除 |
delete() |
toList |
|
| 添加頁面 |
addUI() |
addUI |
addUI.jsp |
| 添加 |
add() |
toList |
|
| 修改頁面 |
editUI() |
editUI |
editUI.jsp |
| 修改 |
edit() |
toList |
|
toList的配置為:type="redirectAction" actionName=“xxAction_list”
<result name="toList" type="redirectAction">role_list</result>
實現功能
1,寫Action類,寫Action中的方法,確定Service中的方法。
DepartmentAction.java
@Controller @Scope("prototype") public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{//這里也需要轉遞參數id,名稱和說明,實體里都有了。所以要實現ModelDriven,封裝表單當中傳遞過來的參數 //有接口有實現類,想用它就創建,采用@Resource注入 @Resource private DepartmentService departmentService; private Department model = new Department(); public Department getModel() { return model; } /** * 列表 */ public String list() throws Exception { List<Department> departmentList = departmentService.findAll();//findAll()在DepartmentService接口中創建 ActionContext.getContext().put("departmentList", departmentList);//放在map里面方便拿,#號獲取 return "list"; } /** * 刪除 */ public String delete() throws Exception { departmentService.delete(model.getId());//delete()在DepartmentService接口中創建 return "toList"; } /** * 添加頁面 */ public String addUI() throws Exception { return "saveUI"; } /** * 添加 */ public String add() throws Exception { //封裝到對象中 /*Department department = new Department(); department.setName(model.getName()); department.setDescription(model.getDescription()); //保存到數據庫中 departmentService.save(department);*/ //添加的方法可以簡化成一行代碼 departmentService.save(model); return "toList"; } /** * 修改頁面 */ public String editUI() throws Exception { //准備回顯的數據 Department department =departmentService.getById(model.getId());//把回顯的數據的id告訴我 ActionContext.getContext().getValueStack().push(department);//把它放到棧頂就能回顯 return "saveUI"; } /** * 修改 */ public String edit() throws Exception { //1.從數據庫中取出原對象 Department department = departmentService.getById(model.getId()); //2.設置要修改的屬性 department.setName(model.getName()); department.setDescription(model.getDescription()); //3.更新到數據庫中 departmentService.update(department); return "toList"; } }
在struts.xml文件中配置
<!-- 部門管理 --> <action name="department_*" class="departmentAction" method="{1}"> <result name="list">/WEB-INF/jsp/departmentAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/departmentAction/saveUI.jsp</result> <result name="toList" type="redirectAction">department_list</result> </action>
要想找到這個bean,得在DepartmetAction類上聲明:
@Controller
@Scope("prototype")
2,寫Service方法,確定Dao中的方法。
DepartmentService.java
//接口中只有方法的聲明,沒有方法的實現 public interface DepartmentService { List<Department> findAll(); void delete(Long id); void save(Department model); Department getById(Long id); void update(Department department); }
DepartmnetServiceImpl.java
//在Action中要調用Service,要寫下面兩個注解 @Service @Transactional //業務層要管理實務,控制開關事務 public class DepartmentServiceImpl implements DepartmentService{ //Service里要調用Dao,得到它通過注入 @Resource private DepartmentDao departmentDao; public List<Department> findAll() { return departmentDao.findAll(); } public void delete(Long id) { departmentDao.delete(id); } public void save(Department model) { departmentDao.save(model); } public Department getById(Long id) { return departmentDao.getById(id); } public void update(Department department) { departmentDao.update(department); } }
3,寫Dao方法。
DepartmentDao.java
public interface DepartmentDao extends BaseDao<Department>{ }
DepartmentDaoImpl.java
//加上這個注解交給容器管理,Service就能 @Repository public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao { }
4,寫JSP

把美工做好的頁面代碼拷過來
修改為正確的路徑
list.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <title>部門列表</title> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 7 <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script> 8 <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script> 9 <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script> 10 <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" /> 11 <script type="text/javascript"> 12 </script> 13 </head> 14 <body> 15 16 <div id="Title_bar"> 17 <div id="Title_bar_Head"> 18 <div id="Title_Head"></div> 19 <div id="Title"><!--頁面標題--> 20 <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 部門管理 21 </div> 22 <div id="Title_End"></div> 23 </div> 24 </div> 25 26 <div id="MainArea"> 27 <table cellspacing="0" cellpadding="0" class="TableStyle"> 28 29 <!-- 表頭--> 30 <thead> 31 <tr align=center valign=middle id=TableTitle> 32 <td width="150px">部門名稱</td> 33 <td width="150px">上級部門名稱</td> 34 <td width="200px">職能說明</td> 35 <td>相關操作</td> 36 </tr> 37 </thead> 38 39 <!--顯示數據列表--> 40 <tbody id="TableData" class="dataContainer" datakey="departmentList"> 41 42 <s:iterator value="#departmentList"> 43 <tr class="TableDetail1 template"> 44 <td>${name} </td> 45 <td>${parent.name} </td> 46 <td>${description} </td> 47 <td><s:a action="department_delete?id=%{id}" onClick="return window.confirm('這將刪除所有的下級部門,您確定要刪除嗎?')" >刪除</s:a> 48 <s:a action="department_editUI?id=%{id}">修改</s:a> 49 </td> 50 </tr> 51 </s:iterator> 52 </tbody> 53 </table> 54 55 <!-- 其他功能超鏈接 --> 56 <div id="TableTail"> 57 <div id="TableTail_inside"> 58 <s:a action="department_addUI"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></s:a> 59 </div> 60 </div> 61 </div> 62 63 <!--說明--> 64 <div id="Description"> 65 說明:<br /> 66 1,列表頁面只顯示一層的(同級的)部門數據,默認顯示最頂級的部門列表。<br /> 67 2,點擊部門名稱,可以查看此部門相應的下級部門列表。<br /> 68 3,刪除部門時,同時刪除此部門的所有下級部門。 69 </div> 70 </body> 71 </html>
saveUI.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <title>部門設置</title> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 7 <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script> 8 <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script> 9 <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script> 10 <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" /> 11 </head> 12 <body> 13 14 <!-- 標題顯示 --> 15 <div id="Title_bar"> 16 <div id="Title_bar_Head"> 17 <div id="Title_Head"></div> 18 <div id="Title"><!--頁面標題--> 19 <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 部門信息 20 </div> 21 <div id="Title_End"></div> 22 </div> 23 </div> 24 25 <!--顯示表單內容--> 26 <div id=MainArea> 27 <s:form action="department_%{id == null ? 'add' : 'edit'}"><!-- 提交到 --> 28 <s:hidden name="id"></s:hidden> 29 30 <div class="ItemBlock_Title1"><!-- 信息說明<DIV CLASS="ItemBlock_Title1"> 31 <IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 部門信息 </DIV> --> 32 </div> 33 34 <!-- 表單內容顯示 --> 35 <div class="ItemBlockBorder"> 36 <div class="ItemBlock"> 37 <table cellpadding="0" cellspacing="0" class="mainForm"> 38 <tr><td width="100">上級部門</td> 39 <td><select name="parentId" class="SelectStyle"> 40 <option value="0" selected="selected">請選擇部門</option> 41 <option value="7">┠總經理室</option> 42 <option value="1">┠市場部</option> 43 <option value="2"> ┠咨詢部</option> 44 <option value="3"> ┠招生部</option> 45 <option value="4">┠教學部</option> 46 <option value="5">┠后勤部</option> 47 </select> 48 </td> 49 </tr> 50 <tr><td>部門名稱</td> 51 <td><s:textfield name="name" cssClass="InputStyle"/> *</td> 52 </tr> 53 <tr><td>職能說明</td> 54 <td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td> 55 </tr> 56 </table> 57 </div> 58 </div> 59 60 <!-- 表單操作 --> 61 <div id="InputDetailBar"> 62 <input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/> 63 <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a> 64 </div> 65 </s:form> 66 </div> 67 68 <div class="Description"> 69 說明:<br /> 70 1,上級部門的列表是有層次結構的(樹形)。<br/> 71 2,如果是修改:上級部門列表中不能顯示當前修改的部門及其子孫部門。因為不能選擇自已或自已的子部門作為上級部門。<br /> 72 </div> 73 </body> 74 </html>
