系统管理模块

  1. 需求分析

  2. 数据库表的设计

    1. 少关联,避免关系复杂
    2. 心状设计,多个主表分模块设计,主表之间使用中间表关联
  3. 写持久化类和映射文件(数据库表的搭建)

    • User.java

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      package cn.itcast.shoa.domain.system;

      import java.io.Serializable;
      import java.util.Set;

      public class User implements Serializable{
      private long uid;
      private String username;
      private String password;
      private String sex;
      private String phone;
      private String email;

      private Set<Role> roles;

      private Department department;

      public long getUid() {
      return uid;
      }

      public void setUid(long uid) {
      this.uid = uid;
      }

      public String getUsername() {
      return username;
      }

      public void setUsername(String username) {
      this.username = username;
      }

      public String getPassword() {
      return password;
      }

      public void setPassword(String password) {
      this.password = password;
      }

      public String getSex() {
      return sex;
      }

      public void setSex(String sex) {
      this.sex = sex;
      }

      public String getPhone() {
      return phone;
      }

      public void setPhone(String phone) {
      this.phone = phone;
      }

      public String getEmail() {
      return email;
      }

      public void setEmail(String email) {
      this.email = email;
      }

      public Set<Role> getRoles() {
      return roles;
      }

      public void setRoles(Set<Role> roles) {
      this.roles = roles;
      }

      public Department getDepartment() {
      return department;
      }

      public void setDepartment(Department department) {
      this.department = department;
      }
      }
    • User.hbm.xml

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      <?xml version="1.0" encoding="utf-8"?>
      <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
      <hibernate-mapping>
      <class name="cn.itcast.shoa.domain.system.User">
      <!-- class描述持久化类,name持久化类的名称 -->
      <id name="uid" length="5">
      <generator class="increment"></generator>
      </id>
      <property name="username" length="20"></property>
      <property name="password" length="20"></property>
      <property name="email" length="20"></property>
      <property name="phone" length="11"></property>
      <property name="sex" length="20"></property>

      <set name="roles" table="user_role">
      <key>
      <column name="uid"></column>
      </key>
      <many-to-many class="cn.itcast.shoa.domain.system.Role" column="rid"></many-to-many>
      </set>

      <many-to-one name="department" column="did" class="cn.itcast.shoa.domain.system.Department"></many-to-one>
      </class>
      </hibernate-mapping>
    • Department.java

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      package cn.itcast.shoa.domain.system;

      import java.io.Serializable;
      import java.util.Set;

      public class Department implements Serializable{
      private long did;//标示符
      private String name;//部门名称
      private String description;//部门职能
      private Set<User> users;

      public long getDid() {
      return did;
      }
      public void setDid(long did) {
      this.did = did;
      }
      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;
      }
      public Set<User> getUsers() {
      return users;
      }
      public void setUsers(Set<User> users) {
      this.users = users;
      }
      }
    • Department.hbm.xml

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      <?xml version="1.0" encoding="utf-8"?>
      <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
      <hibernate-mapping>
      <class name="cn.itcast.shoa.domain.system.Department">
      <!-- class描述持久化类,name持久化类的名称 -->
      <id name="did" length="5">
      <generator class="increment"></generator>
      </id>
      <property name="name" length="20"></property>
      <property name="description" length="20"></property>
      <!-- inverse 一对多维护 多的一方维护 -->
      <set name="users" inverse="true">
      <key>
      <column name="did"></column>
      </key>
      <one-to-many class="cn.itcast.shoa.domain.system.User"/>
      </set>
      </class>
      </hibernate-mapping>
    • Role.java

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      package cn.itcast.shoa.domain.system;

      import java.io.Serializable;
      import java.util.Set;

      public class Role implements Serializable{
      private Long rid;//标示符
      private String name;//名称
      private String description;//角色描述
      private Set<User> users;

      public Long getRid() {
      return rid;
      }
      public void setRid(Long rid) {
      this.rid = rid;
      }
      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;
      }
      public Set<User> getUsers() {
      return users;
      }
      public void setUsers(Set<User> users) {
      this.users = users;
      }
      }
    • Role.hbm.xml

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      <?xml version="1.0" encoding="utf-8"?>
      <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
      <hibernate-mapping>
      <class name="cn.itcast.shoa.domain.system.Role">
      <!-- class描述持久化类,name持久化类的名称 -->
      <id name="rid" length="5">
      <generator class="increment"></generator>
      </id>
      <property name="name" length="20"></property>
      <property name="description" length="20"></property>
      <!-- 多对多谁维护效率都一样 -->
      <set name="users" table="user_role">
      <key>
      <column name="uid"></column>
      </key>
      <many-to-many class="cn.itcast.shoa.domain.system.User"></many-to-many>
      </set>
      </class>
      </hibernate-mapping>
    • 讲hbm.xml文件添加到hibernate.cfg.xml文件中

      1
      2
      3
      4
      5
      6
      7
      8
      <!-- 
      引入映射文件
      -->
      <property name="show_sql">true</property>
      <mapping resource="cn/itcast/shoa/domain/Person.hbm.xml" />
      <mapping resource="cn/itcast/shoa/domain/system/Department.hbm.xml" />
      <mapping resource="cn/itcast/shoa/domain/system/Role.hbm.xml" />
      <mapping resource="cn/itcast/shoa/domain/system/User.hbm.xml" />
  4. 写部门模块

    1. 写dao和service层的接口和类

      • dao

        1
        2
        3
        4
        5
        6
        7
        8
        package cn.itcast.shoa.dao;

        import cn.itcast.shoa.dao.base.BaseDao;
        import cn.itcast.shoa.domain.system.Department;

        public interface DepartmentDao extends BaseDao<Department>{

        }
      • daoImpl

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        package cn.itcast.shoa.dao.impl;

        import javax.annotation.Resource;

        import org.springframework.stereotype.Repository;

        import cn.itcast.shoa.dao.DepartmentDao;
        import cn.itcast.shoa.dao.base.impl.BaseDaoImpl;
        import cn.itcast.shoa.domain.system.Department;

        @Repository("departmentDao")
        public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao{

        }
      • service

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        package cn.itcast.shoa.service;

        import java.util.Collection;

        import cn.itcast.shoa.domain.system.Department;

        public interface DepartmentService{
        //Collection集合的最顶级接口
        public Collection<Department> getAllDepartments();

        }
      • serviceImpl

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        package cn.itcast.shoa.service.impl;

        import java.util.Collection;

        import javax.annotation.Resource;

        import org.springframework.stereotype.Service;

        import cn.itcast.shoa.dao.DepartmentDao;
        import cn.itcast.shoa.domain.system.Department;
        import cn.itcast.shoa.service.DepartmentService;

        @Service("departmentService")
        public class DepartmentServiceImpl implements DepartmentService{
        @Resource(name = "departmentDao")
        private DepartmentDao departmentDao;

        @Override
        public Collection<Department> getAllDepartments() {
        // TODO Auto-generated method stub
        return this.departmentDao.getAllEntry();
        }
        }
    2. 写action类

      • 修改BeanAction

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        55
        56
        57
        58
        59
        60
        61
        62
        package cn.itcast.shoa.struts.action.base;

        import java.lang.reflect.ParameterizedType;

        import com.opensymphony.xwork2.ActionSupport;
        import com.opensymphony.xwork2.ModelDriven;

        public class BaseAction<E> extends ActionSupport implements ModelDriven<E>{

        private Class classt;
        private E e;

        public BaseAction(){
        ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
        this.classt = (Class)type.getActualTypeArguments()[0];
        try {
        this.e = (E)this.classt.newInstance();
        } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        }

        @Override
        public E getModel() {
        // TODO Auto-generated method stub
        return this.e;
        }


        public static final String LISTACTION = "listAction";

        /**
        * 跳转了列表页面
        */
        public static String listAction = LISTACTION;

        public static final String UPDATE_UI = "updateUI";

        /*
        * 跳转到修改界面
        */
        public static String updateUI = UPDATE_UI;

        public static final String ADD_UI = "addUI";

        /**
        * 跳转到添加的页面
        */
        public static String addUI = ADD_UI;


        public static final String ACTION2ACTION = "action2action";

        /**
        * 由action跳转到action
        */
        public static String action2action = ACTION2ACTION;
        }
      • 编写action

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        package cn.itcast.shoa.struts.action;

        import java.util.Collection;

        import javax.annotation.Resource;

        import org.springframework.context.annotation.Scope;
        import org.springframework.stereotype.Controller;

        import com.opensymphony.xwork2.ActionContext;

        import cn.itcast.shoa.domain.system.Department;
        import cn.itcast.shoa.service.DepartmentService;
        import cn.itcast.shoa.struts.action.base.BaseAction;

        @Controller("departmentAction")
        @Scope("prototype")
        public class DepartmentAction extends BaseAction<Department>{
        @Resource(name="departmentService")
        private DepartmentService departmentService;

        //方法名最好不以get开头
        public String showAllDepartment() {
        //list集合必须放入值栈中才能用ognl表达式获取数据 两种做法:对象栈与map栈
        Collection<Department> departmentsList = this.departmentService.getAllDepartments();
        //放入map
        ActionContext.getContext().put("departmentList", departmentsList);
        return listAction;
        }
        }
    3. 编写struts2的配置文件

      • 编写新的配置文件struts-department.xml放在struts文件夹下

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

        <struts>
        <package name="department" namespace="/" extends="struts-default">
        <action name="departmentAction_*" method="{1}" class="departmentAction">
        <result>暂时不写</result>
        </action>
        </package>
        </struts>

      • 修改struts.xml配置文件

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

        <struts>
        <constant name="struts.devMode" value="true"/>
        <constant name="struts.ui.theme" value="simple"/>

        <include file="struts/struts-person.xml"></include>
        <include file="struts/struts-department.xml"></include>
        </struts>
    4. 编写jsp页面,所有的jsp页面都在web-inf下建立

      conmmon处理公共的js和css,system项目模块,css存放样式。

    5. 在jsp/system/department下,创建一个list.jsp

      • 把样式进行替换引入公共的jsp文件

        1
        <%@ include file="/WEB-INF/jsp/common/common.jsp" %>
      • 编写heml内容

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
      <%@ include file="/WEB-INF/jsp/common/common.jsp" %>
      <html>
      <head>
      <title>部门列表</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      </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="${pageContext.request.contextPath}/css/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="200px">职能说明</td>
      <td>相关操作</td>
      </tr>
      </thead>

      <!--显示数据列表-->
      <tbody id="TableData" class="dataContainer" datakey="departmentList">
      <s:iterator value="#departmentList">
      <tr class="TableDetail1 template">
      <td><s:property value="name"/></td>
      <td><s:property value="description"/></td>
      <td><a onClick="return window.confirm('这将删除所有的下级部门,您确定要删除吗?')" href="#">删除</a>
      <a href="saveUI.html">修改</a>
      </td>
      </tr>
      </s:iterator>

      <s:iterator value="#dList">
      <s:debug></s:debug>
      <s:property value="name"/>
      </s:iterator>

      <!--
      Map<String,Department>
      -->
      <s:iterator value="#map">
      <s:property value="key"/>
      <s:property value="value.name"/>
      </s:iterator>
      <!--
      List<Map<String,Department>>
      -->
      <s:iterator value="#listMap">
      <!--
      list集合中当前迭代的元素在栈顶,所以map在栈顶,如果s:iterator中的value属性不写,默认迭代栈顶的元素
      -->
      <s:iterator>
      <s:property value="key"/>
      <s:property value="value.name"/>
      </s:iterator>
      </s:iterator>
      <!--
      Map<String,List<Department>>
      -->
      <s:iterator value="#mapList">
      <s:property value="key"/>
      <s:iterator value="value">
      <s:property value="name"/>
      </s:iterator>
      </s:iterator>
      <!--
      List<Map<String,List<Department>>>
      -->
      <s:iterator value="#listMapList">
      <s:iterator>
      <s:property value="key"/>
      <s:iterator value="value">
      <s:property value="name"/>
      </s:iterator>
      </s:iterator>
      </s:iterator>
      </tbody>
      </table>

      <!-- 其他功能超链接 -->
      <div id="TableTail">
      <div id="TableTail_inside">
      <a href="saveUI.html"><img src="${pageContext.request.contextPath}/css/images/createNew.png" /></a>
      </div>
      </div>
      </div>

      <!--说明-->
      <div id="Description">
      说明:<br />
      1,列表页面只显示一层的(同级的)部门数据,默认显示最顶级的部门列表。<br />
      2,点击部门名称,可以查看此部门相应的下级部门列表。<br />
      3,删除部门时,同时删除此部门的所有下级部门。
      </div>

      </body>
      </html>
    6. 修改struts2配置文件struts-department.xml

      1
      2
      3
      4
      5
      6
      7
      <struts>
      <package name="department" namespace="/" extends="struts-default">
      <action name="departmentAction_*" method="{1}" class="departmentAction">
      <result name="listAction">/WEB-INF/jsp/system/department/list.jsp</result>
      </action>
      </package>
      </struts>
    7. 浏览器测试http://localhost:8080/shoa_maven/departmentAction_showAllDepartment.action

      此时数据库无数据,自行在department表中添加数据

值栈

  1. 概念

    struts在后台存放数据的内存结构

  2. 生命周期

    一次请求

  3. 值栈的内存结构

    1. 对象栈
      • action
    2. map栈
      • request
      • session
      • application
  4. ognl表达式对于对象栈的数据直接根据key值获取value值就可以了,map栈的数据,加#访问。

  5. 对象栈的操作

    • 把一个对象放入到对象栈

      1
      2
      3
      4
      5
      6
      7
      8
      9
      //放入到栈顶
      ActionContext.getContext().getValueStack().push
      ActionContext.getContext().getValueStack().getRoot().add(0,obj)
      //放入到栈底
      ActionContext.getContext().getValueStack().getRoot().add
      //把一个栈顶的元素弹出
      ActionContext.getContext().getValueStack().pop();
      //得到栈顶的元素
      ActionContext.getContext().getValueStack().peek();

      如果把一个对象放入对象栈中,可以通过访问其属性直接访问其值。

  6. 案例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ActionContext.getContext().put("departmentsList", departmentsList);
    把departmentsList放入map栈中,并取名为"departmentsList"
    //value的值是要迭代的集合在哪
    //该标签有一个规则:当前正在迭代的元素在栈顶
    <s:iterator value="#departmentList">
    <tr class="TableDetail1 template">
    <td><s:property value="name"/></td>
    <td><s:property value="description"/></td>
    <td><a onClick="return window.confirm('这将删除所有的下级部门,您确定要删除吗?')" href="#">删除</a>
    <a href="saveUI.html">修改</a>
    </td>
    </tr>
    </s:iterator>

    测试map中迭代的元素在栈顶

    1
    2
    3
    4
    5
    6
    7
    8
    9
    //测试map中迭代的元素在栈顶
    public String showList() {
    Department department = new Department();
    department .setName("华沙");
    List<Department> dList = new ArrayList<Department>();
    dList.add(department);
    ActionContext.getContext().put("dlist", dList);
    return listAction;
    }
    1
    2
    3
    4
    <s:iterator value="#dlist">
    <s:debug></s:debug>
    <s:property value="name"/>
    </s:iterator>

权限管理

审批流转