zoukankan      html  css  js  c++  java
  • 实战3--设计管理模块, 第4步, 新增模块, 部门管理

    一... 设计实体类/表

    1. 写实体类Department.java:

    package cn.itcast.oa.domain;
    
    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

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="cn.itcast.oa.domain">
    	<class name="Department" table="itcast_department">
    		<id name="id">
    			<generator class="native"></generator>
    		</id>
    		<property name="name" />
    		<property name="description" />
    	</class>
    </hibernate-mapping>
    

    3. 把实体类加入到hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    	<session-factory>
    		<!-- Database connection settings -->
    		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- 2. other configuration --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="connection.pool_size">1</property> <!--3. mapping --> <mapping resource="cn/itcast/oa/domain/User.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Role.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Department.hbm.xml" /> </session-factory> </hibernate-configuration>

    4. 测试类生成表:SpringTest.java

    package cn.itcast.oa.test;
    
    import org.hibernate.SessionFactory;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
    	private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    	@Test
    	public void testSessionFactory() throws Exception{
    		SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
    		System.out.println(sessionFactory);
    		
    	}	
    }
    

    确认生成表成功!!!  

    二.. 分析有几个功能, 对应几个请求

    1. 搭action框架

        注意: @Controller  @Scope("prototype")

    package cn.itcast.oa.view.action;
    import javax.annotation.Resource;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    @Controller @Scope("prototype") public class DepartmentAction extends ActionSupport{ /*列表*/ public String list() throws Exception { return "list"; } /*删除*/ public String delete() throws Exception { return "toList"; } /*添加页面*/ public String addUI() throws Exception { return "saveUI"; } /*添加*/ public String add() throws Exception { return "toList"; } /*修改页面*/ public String editUI() throws Exception { return "saveUI"; } /*修改*/ public String edit() throws Exception { return "toList"; } }

    2. 配置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> 
    

    3. 写两个简单的jsp文件, 验证成功

    4. 开始完善action:

    package cn.itcast.oa.view.action;
    
    import java.util.List;
    
    import javax.annotation.Resource;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    
    import cn.itcast.oa.domain.Department;
    import cn.itcast.oa.service.DepartmentService;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    
    @Controller
    @Scope("prototype")
    public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{
    	
    	@Resource
    	private DepartmentService departmentService;
    	
    	private Department model  = new Department();
    	/*列表*/
    	public String list() throws Exception {
    		List<Department> departmentList = departmentService.findAll();
    		ActionContext.getContext().put("departmentList", departmentList);
    		return "list";
    	}
    	/*删除*/
    	public String delete() throws Exception {
    		departmentService.delete(model.getId());
    		return "toList";
    	}
    	/*添加页面*/
    	public String addUI() throws Exception {		
    		return "saveUI";
    	}
    	/*添加*/
    	public String add() throws Exception {
    		departmentService.save(model);
    		return "toList";
    	}
    	/*修改页面*/
    	public String editUI() throws Exception {
    		/*准备回显数据*/
    		Department department = departmentService.getById(model.getId());
    		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";
    	}
    	
    	public Department getModel() {
    		// TODO Auto-generated method stub
    		return model;
    	}
    }
    

    5. 写service接口, DepartmentService.java, 是在action里调用方法的时候ctrl+1创建的方法

    package cn.itcast.oa.service;
    import java.util.List;
    import cn.itcast.oa.domain.Department;
    
    public interface DepartmentService {
    	List<Department> findAll();
    	void delete(Long id);
    	void save(Department model);
    	Department getById(Long id);
    	void update(Department department);
    }
    

    6. 创建DepartmentService接口的实现类, DepartmentServiceImpl.java

        注意@Service, @Transactional, 用Dao的时候用@Resource

    package cn.itcast.oa.service.impl;
    import java.util.List;
    import javax.annotation.Resource;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import cn.itcast.oa.dao.DepartmentDao;
    import cn.itcast.oa.domain.Department;
    import cn.itcast.oa.service.DepartmentService;
    
    @Service
    @Transactional
    public class DepartmentServiceImpl implements DepartmentService{
    	@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);
    	}
    }
    

    三.. 创建Dao接口和实现类

    1. Dao接口, DepartmentDao.java, 注意要继承BaseDao接口

    package cn.itcast.oa.dao;
    import cn.itcast.oa.base.BaseDao;
    import cn.itcast.oa.domain.Department;
    public interface DepartmentDao extends BaseDao<Department>{
    }

    2. 实现类DepartmentDaoImpl.java,

       注意1. 继承BaseDaoImpl, 2. 实现DepartmentDao 3. @Repository

    package cn.itcast.oa.dao.impl;
    import org.springframework.stereotype.Repository;
    import cn.itcast.oa.base.BaseDaoImpl;
    import cn.itcast.oa.dao.DepartmentDao;
    import cn.itcast.oa.domain.Department;
    @Repository
    public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao{
    
    }
    

      

    四.. 写jsp页面:

    新建jsp路径下的departmentAction文件夹, 创建两个jsp文件夹, list.jsp,和 saveUI.jsp

    list.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%String path = request.getContextPath();%>
    <html>
    <head>
        <title>部门列表</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
           <script language="javascript" type="text/javascript" src=<%=path%>/script/jquery.js"></script>
        <script language="javascript" type="text/javascript" src=<%=path%>/script/pageCommon.js"></script>
        <script language="javascript" type="text/javascript" src=<%=path%>/script/PageUtils.js"></script>
       
        <link type="text/css" rel="stylesheet" href="<%=path%>/style/blue/pageCommon.css"/>
        <script type="text/javascript">
        </script>
    </head>
    <body>
     
    <div id="Title_bar">
        <div id="Title_bar_Head">
            <div id="Title_Head"></div>
            <div id="Title"><!--页面标题-->
                <img border="0" width="13" height="13" src="<%=path%>/style/images/title_arrow.gif"/> 部门管理
            </div>
            <div id="Title_End"></div>
        </div>
    </div>
    
    <div id="MainArea">
        <table cellspacing="0" cellpadding="0" class="TableStyle">
           
            <!-- 表头-->
            <thead>
                <tr align=center valign=middle id=TableTitle>
                	<td width="150px">部门名称</td>
    				<td width="150px">上级部门名称</td>
    				<td width="200px">职能说明</td>
    				<td>相关操作</td>
                </tr>
            </thead>
    
    		<!--显示数据列表-->
            <tbody id="TableData" class="dataContainer" datakey="departmentList">
            
            <s:iterator value="#departmentList">
    			<tr class="TableDetail1 template">
    				<td>${name} </td>
    				<td>${parent.name} </td>
    				<td>${description} </td>
    				<td>
    					<s:a action="department_delete?id=%{id}" onclick="return window.confirm('这将删除所有的下级部门,您确定要删除吗?')">删除</s:a>
    					<s:a action="department_editUI?id=%{id}">修改</s:a>
    				</td>
    			</tr>
    		</s:iterator>	
    			
            </tbody>
        </table>
        
        <!-- 其他功能超链接 -->
        <div id="TableTail">
            <div id="TableTail_inside">
                <s:a action="department_addUI"><img src="<%=path%>/style/images/createNew.png" /></s:a>
            </div>
        </div>
    </div>
    
    <!--说明-->	
    <div id="Description"> 
    	说明:<br />
    	1,列表页面只显示一层的(同级的)部门数据,默认显示最顶级的部门列表。<br />
    	2,点击部门名称,可以查看此部门相应的下级部门列表。<br />
    	3,删除部门时,同时删除此部门的所有下级部门。
    </div>
    
    </body>
    </html>
    

    saveUI.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%String path = request.getContextPath();%>
    <html>
    <head>
    	<title>部门设置</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script language="javascript" type="text/javascript" src=<%=path%>/script/jquery.js"></script>
        <script language="javascript" type="text/javascript" src=<%=path%>/script/pageCommon.js"></script>
        <script language="javascript" type="text/javascript" src=<%=path%>/script/PageUtils.js"></script>
        <link type="text/css" rel="stylesheet" href="<%=path%>/style/blue/pageCommon.css"/>
    </head>
    <body>
    
    <!-- 标题显示 --> 
    <div id="Title_bar">
        <div id="Title_bar_Head">
            <div id="Title_Head"></div>
            <div id="Title"><!--页面标题-->
                <img border="0" width="13" height="13" src="<%=path%>/style/images/title_arrow.gif"/> 部门信息
            </div>
            <div id="Title_End"></div>
        </div>
    </div>
    
    <!--显示表单内容-->
    <div id=MainArea>
    
        <s:form action="department_%{id == null ? 'add' : 'edit'}">
            <s:hidden name="id"></s:hidden>
            
            <div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1">
            	<IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 部门信息 </DIV>  -->
            </div>
            
            <!-- 表单内容显示 -->
            <div class="ItemBlockBorder">
                <div class="ItemBlock">
                    <table cellpadding="0" cellspacing="0" class="mainForm">
                        <tr><td width="100">上级部门</td>
                            <td><select name="parentId" class="SelectStyle">
                                    <option value="">请选择部门</option>
                                    <option value="7">┠总经理室</option>
                                    <option value="1">┠市场部</option>
                                    <option value="2"> ┠咨询部</option>
                                    <option value="3"> ┠招生部</option>
                                    <option value="4">┠教学部</option>
                                    <option value="5">┠后勤部</option>
                                </select>
                            </td>
                        </tr>
                        <tr><td>部门名称</td>
                            <td><s:textfield  name="name" cssClass="InputStyle"/> *</td>
                        </tr>
                        <tr><td>职能说明</td>
                            <td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td>
                        </tr>
                    </table>
                </div>
            </div>
            
            <!-- 表单操作 -->
            <div id="InputDetailBar">
                <input type="image" src="<%=path%>/style/images/save.png"/>
                <a href="javascript:history.go(-1);"><img src="<%=path%>/style/images/goBack.png"/></a>
            </div>
        </s:form>
    </div>
    
    <div class="Description">
    	说明:<br />
    	1,上级部门的列表是有层次结构的(树形)。<br/>
    	2,如果是修改:上级部门列表中不能显示当前修改的部门及其子孙部门。因为不能选择自已或自已的子部门作为上级部门。<br />
    </div>
    
    </body>
    </html>
    

      

      

      

     

  • 相关阅读:
    [unity3d程序] 纹理扩散
    深入浅出SharePoint——自定义带ECB列
    深入浅出TFS——工作区Workspace
    深入浅出SharePoint——部署WSP
    深入浅出SharePoint——批处理高效导入数据
    深入浅出SharePoint——Log4net应用
    遍历文件夹及文件
    深入浅出SharePoint——使用CAML定制View
    拷贝文件到另一台电脑
    深入浅出SharePoint——在自定义表单中使用上传附件控件
  • 原文地址:https://www.cnblogs.com/wujixing/p/5501960.html
Copyright © 2011-2022 走看看