首注:本学习教程为传智播客汤阳光讲师所公布的免费OA项目视频的文字版,本人用此来加强巩固自己开发知识,如有网友转载,请注明。谢谢。
一 根据需求,根据前台页面功能设计实体,创建javabean。Department.java,内容如下(先不处理上下级部门):
1 package cn.clear.oa.domain; 2 3 public class Department { 4 5 private Long id; 6 private String name; 7 private String description; 8 9 public Long getId() { 10 return id; 11 } 12 public void setId(Long id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public String getDescription() { 22 return description; 23 } 24 public void setDescription(String description) { 25 this.description = description; 26 } 27 28 29 }
Department.java
二 根据实体创建映射文件Department.hbm.xml,内容如下:
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 </class> 12 </hibernate-mapping>
Department.hbm.xml
三 在hibernate.cfg.xml中配置映射文件,内容为:
<mapping resource="cn/clear/oa/domain/Department.hbm.xml" />
四 分析功能及有几个请求:(详情见OA项目5文章中对增删改查的分析及列表,以后增删改查就采用那套模板)
五 建立DepartmentAction.java,内容如下:
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 @Resource 22 private DepartmentService departmentService; 23 private Department model = new Department(); 24 25 public Department getModel() { 26 // TODO Auto-generated method stub 27 return model; 28 } 29 30 31 public String list() throws Exception { 32 33 List<Department> departmentList = departmentService.findAll(); 34 ActionContext.getContext().put("departmentList", departmentList); 35 return "list"; 36 } 37 public String delete() throws Exception { 38 39 departmentService.delete(model.getId()); 40 41 return "toList"; 42 } 43 public String add() throws Exception { 44 departmentService.save(model); 45 return "toList"; 46 } 47 public String addUI() throws Exception { 48 49 return "saveUI"; 50 } 51 public String edit() throws Exception { 52 53 Department department = departmentService.findById(model.getId()); 54 department.setName(model.getName()); 55 department.setDescription(model.getDescription()); 56 departmentService.update(department); 57 return "toList"; 58 } 59 public String editUI() throws Exception { 60 61 Department department = departmentService.findById(model.getId()); 62 //将对象放在栈顶 63 ActionContext.getContext().getValueStack().push(department); 64 65 return "saveUI"; 66 } 67 68 }
DepartmentAction.java
根据action添加struts.xml的action,具体如下:
1 <!-- 部门管理 --> 2 <action name="department_*" class="departmentAction" method="{1}"> 3 <result name="list">/WEB-INF/jsp/departmentAction/list.jsp</result> 4 <!-- 类型为重定向 --> 5 <result name="toList" type="redirectAction">department_list</result> 6 <result name="saveUI">/WEB-INF/jsp/departmentAction/saveUI.jsp</result> 7 </action>
struts.xml片段
六 建立DepartmentService.java与DepartmentServiceImpl.java:
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 }
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.springframework.stereotype.Service; 8 import org.springframework.transaction.annotation.Transactional; 9 10 import cn.clear.oa.dao.DepartmentDao; 11 import cn.clear.oa.domain.Department; 12 import cn.clear.oa.service.DepartmentService; 13 @Service 14 @Transactional 15 public class DepartmentServiceImpl implements DepartmentService{ 16 17 @Resource 18 private DepartmentDao departmentDao; 19 20 public List<Department> findAll() { 21 22 return departmentDao.findAll(); 23 } 24 25 public void delete(Long id) { 26 27 departmentDao.delete(id); 28 29 } 30 31 public void save(Department department) { 32 33 departmentDao.save(department); 34 } 35 36 public Department findById(Long id) { 37 38 return departmentDao.findById(id); 39 } 40 41 public void update(Department department) { 42 43 departmentDao.update(department); 44 } 45 46 }
DepartmentServiceImpl.java
七 建立DepartmentDao.java与DepartmentDaoImpl.java:
1 package cn.clear.oa.dao; 2 3 import cn.clear.oa.base.BaseDao; 4 import cn.clear.oa.domain.Department; 5 6 public interface DepartmentDao extends BaseDao<Department>{ 7 8 }
DepartmentDao.java
1 package cn.clear.oa.dao.impl; 2 3 import org.springframework.stereotype.Repository; 4 5 import cn.clear.oa.base.BaseDaoImpl; 6 import cn.clear.oa.dao.DepartmentDao; 7 import cn.clear.oa.domain.Department; 8 9 @Repository 10 public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao { 11 12 }
DepartmentDaoImpl.java
八 建立前台列表页面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 <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" datakey="departmentList"> 43 <s:iterator value="#departmentList"> 44 <tr class="TableDetail1 template"> 45 <td><a href="_list_level2.html">${name}</a> </td> 46 <td>${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
九 建立前台保存页面saveUI.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><select name="parentId" class="SelectStyle"> 41 <option value="0" selected="selected">请选择部门</option> 42 <option value="7">┠总经理室</option> 43 <option value="1">┠市场部</option> 44 <option value="2"> ┠咨询部</option> 45 <option value="3"> ┠招生部</option> 46 <option value="4">┠教学部</option> 47 <option value="5">┠后勤部</option> 48 </select> 49 </td> 50 </tr> 51 <tr><td>部门名称</td> 52 <td><s:textfield name="name" cssClass="InputStyle"/> *</td> 53 </tr> 54 <tr><td>职能说明</td> 55 <td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td> 56 </tr> 57 </table> 58 </div> 59 </div> 60 61 <!-- 表单操作 --> 62 <div id="InputDetailBar"> 63 <input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/> 64 <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a> 65 </div> 66 </s:form> 67 </div> 68 69 <div class="Description"> 70 说明:<br /> 71 1,上级部门的列表是有层次结构的(树形)。<br/> 72 2,如果是修改:上级部门列表中不能显示当前修改的部门及其子孙部门。因为不能选择自已或自已的子部门作为上级部门。<br /> 73 </div> 74 75 </body> 76 </html>
saveUI.jsp
最后启动服务器,对增删改查功能逐个测试,完成部门功能的基础功能。
时间: 2024-10-04 07:28:36