• 基于SSH2的OA项目1.1_20161207_业务开发


    1.1建立用户的pojo模型

    建立user.java

     1 package org.guangsoft.pojo;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 /**
     7  * 用户的pojo类
     8  * @author guanghe
     9  *
    10  */
    11 public class User
    12 {
    13     //用户的基本属性
    14     private Integer uid;
    15     private String username;
    16     private String password;
    17     private String sex;
    18     private String email;
    19     private String phone;
    20     
    21     //描述用户和角色的关系(一个用户对应多个角色)
    22     private Set<Role> roles = new HashSet<Role>();
    23     //描述用户和部门的关系(多个用户对应一个部门)
    24     private Dept dept = new Dept();
    25     
    26     public Integer getUid()
    27     {
    28         return uid;
    29     }
    30     public void setUid(Integer uid)
    31     {
    32         this.uid = uid;
    33     }
    34     public String getUsername()
    35     {
    36         return username;
    37     }
    38     public void setUsername(String username)
    39     {
    40         this.username = username;
    41     }
    42     public String getPassword()
    43     {
    44         return password;
    45     }
    46     public void setPassword(String password)
    47     {
    48         this.password = password;
    49     }
    50     public String getSex()
    51     {
    52         return sex;
    53     }
    54     public void setSex(String sex)
    55     {
    56         this.sex = sex;
    57     }
    58     public String getEmail()
    59     {
    60         return email;
    61     }
    62     public void setEmail(String email)
    63     {
    64         this.email = email;
    65     }
    66     public String getPhone()
    67     {
    68         return phone;
    69     }
    70     public void setPhone(String phone)
    71     {
    72         this.phone = phone;
    73     }
    74     public Set<Role> getRoles()
    75     {
    76         return roles;
    77     }
    78     public void setRoles(Set<Role> roles)
    79     {
    80         this.roles = roles;
    81     }
    82     public Dept getDept()
    83     {
    84         return dept;
    85     }
    86     public void setDept(Dept dept)
    87     {
    88         this.dept = dept;
    89     }
    90     
    91 }

    部门pojo

     1 package org.guangsoft.pojo;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 /**
     7  * 定义部门的pojo类
     8  * @author guanghe
     9  *
    10  */
    11 public class Dept
    12 {
    13     //基本属性
    14     private Integer did;
    15     private String dname;
    16     private String ddesc;
    17     
    18     //描述部门和用户的关系(一个部门对应多个用户)
    19     private Set<User> users = new HashSet<User>();
    20     
    21     public Integer getDid()
    22     {
    23         return did;
    24     }
    25     public void setDid(Integer did)
    26     {
    27         this.did = did;
    28     }
    29     
    30     public String getDname()
    31     {
    32         return dname;
    33     }
    34     public void setDname(String dname)
    35     {
    36         this.dname = dname;
    37     }
    38     public String getDdesc()
    39     {
    40         return ddesc;
    41     }
    42     public void setDdesc(String ddesc)
    43     {
    44         this.ddesc = ddesc;
    45     }
    46     public Set<User> getUsers()
    47     {
    48         return users;
    49     }
    50     public void setUsers(Set<User> users)
    51     {
    52         this.users = users;
    53     }
    54     
    55 }

    角色的pojo

     1 package org.guangsoft.pojo;
     2 import java.util.HashSet;
     3 import java.util.Set;
     4 /**
     5  * 定义角色的pojo类
     6  * @author guanghe
     7  */
     8 public class Role
     9 {
    10     //基本属性
    11     private Integer rid;
    12     private String rname;
    13     private String rdesc;
    14     
    15     //描述角色和用户的关系
    16     private Set<User> users = new HashSet<User>();
    17     
    18     public Integer getRid()
    19     {
    20         return rid;
    21     }
    22     public void setRid(Integer rid)
    23     {
    24         this.rid = rid;
    25     }
    26     public String getRname()
    27     {
    28         return rname;
    29     }
    30     public void setRname(String rname)
    31     {
    32         this.rname = rname;
    33     }
    34     public String getRdesc()
    35     {
    36         return rdesc;
    37     }
    38     public void setRdesc(String rdesc)
    39     {
    40         this.rdesc = rdesc;
    41     }
    42     public Set<User> getUsers()
    43     {
    44         return users;
    45     }
    46     public void setUsers(Set<User> users)
    47     {
    48         this.users = users;
    49     }
    50 }

    1.2建立pojo的映射文件

    User.hbm.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC 
     3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="org.guangsoft.pojo">
     7     <class name="User" table="t_user">
     8         <id name="uid" column="uid" type="java.lang.Integer">
     9             <generator class="native"></generator>
    10         </id>
    11         <property name="username" column="username" type="java.lang.String"></property>
    12         <property name="password" column="password" type="java.lang.String"></property>
    13         <property name="email" column="email" type="java.lang.String"></property>
    14         <property name="phone" column="phone" type="java.lang.String"></property>
    15         <property name="sex" column="sex" type="java.lang.String"></property>
    16         <!-- 描述用户和角色的关系 -->
    17         <set name="roles" table="user_role">
    18             <key column="uid"></key>
    19             <many-to-many class="Role" column="rid"/>
    20         </set>
    21         <!-- 描述部门和用户的关系 -->
    22         <many-to-one name="dept" class="Dept" column="did"/>
    23     </class>
    24 </hibernate-mapping>

    Role.hbm.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC 
     3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="org.guangsoft.pojo">
     7     <class name="Role" table="t_role">
     8         <id name="rid" column="rid" type="java.lang.Integer">
     9             <generator class="native"></generator>
    10         </id>
    11         <property name="rname" column="rname" type="java.lang.String"></property>
    12         <property name="rdesc" column="rdesc" type="java.lang.String"></property>
    13         <set name="users" table="user_role" inverse="true">
    14             <key column="rid"></key>
    15             <many-to-many class="User" column="uid"></many-to-many>
    16         </set>
    17     </class>
    18 </hibernate-mapping>

    Dept.hbm.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC 
     3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="org.guangsoft.pojo">
     7     <class name="Dept" table="t_dept">
     8         <id name="did" column="did" type="java.lang.Integer">
     9             <generator class="native"></generator>
    10         </id>
    11         <property name="dname" column="dname" type="java.lang.String"></property>
    12         <property name="ddesc" column="ddesc" type="java.lang.String"></property>
    13         <!-- 描述部门和用户的关系 -->
    14         <set name="users" inverse="true">
    15             <key column="did"></key>
    16             <one-to-many class="User"/>
    17         </set>
    18     </class>
    19 </hibernate-mapping>

    1.3定义Dao接口

    定义UserDao

     1 package org.guangsoft.dao;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.pojo.User;
     6 /**
     7  * 完成用户数据的访问
     8  * @author guanghe
     9  */
    10 public interface UserDao
    11 {
    12     //添加用户数据
    13     public void saveUser(User user);
    14     //查询用户列表
    15     public List<User> getAllUsers();
    16     //删除用户
    17     public void deleteUser(User user);
    18     //修改用户
    19     public void updateUser(User user);
    20     //根据id获取用户
    21     public User getUserById(User user);
    22 }

    定义RoleDao

     1 package org.guangsoft.dao;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.pojo.Role;
     6 /**
     7  * 完成角色数据访问
     8  * @author guanghe
     9  *
    10  */
    11 public interface RoleDao
    12 {
    13     public List<Role> getAllRoles();
    14 }

    定义DeptDao

     1 package org.guangsoft.dao;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.pojo.Dept;
     6 /**
     7  * 实现部门的数据访问
     8  * @author guanghe
     9  *
    10  */
    11 public interface DeptDao
    12 {
    13     //查询所有部门数据
    14     public List<Dept> getAllDepts();
    15 }

    1.4建立Dao接口的实现类

    定义UserDaoImpl

     1 package org.guangsoft.dao.impl;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.dao.UserDao;
     6 import org.guangsoft.pojo.Dept;
     7 import org.guangsoft.pojo.User;
     8 import org.hibernate.SessionFactory;
     9 import org.springframework.beans.factory.annotation.Autowired;
    10 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    11 import org.springframework.stereotype.Repository;
    12 
    13 /**
    14  * 建立dao接口实现类
    15  * extends HibernateDaoSupport
    16  * 完成dao中获得session
    17  * @author guanghe
    18  */
    19 @Repository
    20 public class UserDaoImpl extends HibernateDaoSupport implements UserDao
    21 {
    22     /**
    23      * 给父类注入sessionFactory通过自动装配
    24      * @param sessionFactory
    25      */
    26     @Autowired
    27     public void setSessionFactoryAutoWire(SessionFactory sessionFactory)
    28     {
    29         super.setSessionFactory(sessionFactory);
    30     }
    31     
    32     /**
    33      * 添加用户
    34      */
    35     @Override
    36     public void saveUser(User user)
    37     {
    38         super.getHibernateTemplate().save(user);
    39     }
    40 
    41     @Override
    42     public List<User> getAllUsers()
    43     {
    44         return (List<User>)super.getHibernateTemplate().find("from User");
    45     }
    46 
    47     @Override
    48     public void deleteUser(User user)
    49     {
    50         super.getHibernateTemplate().delete(user);
    51     }
    52 
    53     @Override
    54     public void updateUser(User user)
    55     {
    56         super.getHibernateTemplate().update(user);
    57     }
    58 
    59     @Override
    60     public User getUserById(User user)
    61     {
    62         return (User) super.getHibernateTemplate().find("from User u where u.uid=?", user.getUid()).get(0);
    63     }
    64 
    65 }

    定义RoleDaoImpl

     1 package org.guangsoft.dao.impl;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.dao.RoleDao;
     6 import org.guangsoft.pojo.Dept;
     7 import org.guangsoft.pojo.Role;
     8 import org.hibernate.SessionFactory;
     9 import org.springframework.beans.factory.annotation.Autowired;
    10 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    11 import org.springframework.stereotype.Repository;
    12 
    13 /**
    14  * 建立dao接口实现类
    15  * extends HibernateDaoSupport
    16  * 完成dao中获得session
    17  * @author guanghe
    18  *
    19  */
    20 @Repository
    21 public class RoleDaoImpl extends HibernateDaoSupport implements RoleDao
    22 {
    23     /**
    24      * 给父类注入sessionFactory通过自动装配
    25      * @param sessionFactory
    26      */
    27     @Autowired
    28     public void setSessionFactoryAutoWire(SessionFactory sessionFactory)
    29     {
    30         super.setSessionFactory(sessionFactory);
    31     }
    32     
    33     @Override
    34     public List<Role> getAllRoles()
    35     {
    36         return super.getHibernateTemplate().find("from Role");
    37     }
    38 
    39 }

    定义DeptDaoImpl

     1 package org.guangsoft.dao.impl;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.dao.DeptDao;
     6 import org.guangsoft.pojo.Dept;
     7 import org.hibernate.SessionFactory;
     8 import org.springframework.beans.factory.annotation.Autowired;
     9 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    10 import org.springframework.stereotype.Repository;
    11 
    12 /**
    13  * 建立dao接口实现类
    14  * extends HibernateDaoSupport
    15  * 完成dao中获得session
    16  * @author guanghe
    17  *
    18  */
    19 @Repository
    20 public class DeptDaoImpl extends HibernateDaoSupport implements DeptDao
    21 {
    22     /**
    23      * 给父类注入sessionFactory通过自动装配
    24      * @param sessionFactory
    25      */
    26     @Autowired
    27     public void setSessionFactoryAutoWire(SessionFactory sessionFactory)
    28     {
    29         super.setSessionFactory(sessionFactory);
    30     }
    31     
    32     /**
    33      * 查询部门列表
    34      */
    35     @Override
    36     public List<Dept> getAllDepts()
    37     {
    38         return super.getHibernateTemplate().find("from Dept");
    39     }
    40 
    41 }

    1.5建立Servie接口

     1 package org.guangsoft.service;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.vo.UserVo;
     6 
     7 /**
     8  * 部门的业务接口
     9  * @author guanghe
    10  */
    11 public interface UserService
    12 {
    13     //封装部门集合和角色的集合
    14     public UserVo loadDeptRoleService();
    15     //添加用户信息
    16     public void saveUserService(UserVo userVo);
    17     //加载用户列表
    18     public List<UserVo> loadUserListService();
    19     //删除用户
    20     public void deleteUserService(UserVo userVo);
    21     //修改用户
    22     public void updateUserService(UserVo userVo);
    23     //根据id获得用户
    24     public UserVo getUserByIdService(UserVo userVo);
    25 }

    1.6建立service接口实现类

      1 package org.guangsoft.service.impl;
      2 
      3 import java.util.ArrayList;
      4 import java.util.List;
      5 import java.util.Set;
      6 
      7 import org.apache.commons.beanutils.BeanUtils;
      8 import org.guangsoft.dao.DeptDao;
      9 import org.guangsoft.dao.RoleDao;
     10 import org.guangsoft.dao.UserDao;
     11 import org.guangsoft.pojo.Dept;
     12 import org.guangsoft.pojo.Role;
     13 import org.guangsoft.pojo.User;
     14 import org.guangsoft.service.UserService;
     15 import org.guangsoft.vo.UserVo;
     16 import org.junit.Test;
     17 import org.springframework.beans.factory.annotation.Autowired;
     18 import org.springframework.context.ApplicationContext;
     19 import org.springframework.context.support.ClassPathXmlApplicationContext;
     20 import org.springframework.stereotype.Service;
     21 /**
     22  * 部门业务接口实现类
     23  * @author guanghe
     24  *
     25  */
     26 @Service
     27 public class UserServiceImpl implements UserService
     28 {
     29     //声明dao对象
     30     @Autowired
     31     private UserDao userDao;
     32     @Autowired
     33     private DeptDao deptDao;
     34     @Autowired
     35     private RoleDao roleDao;
     36 
     37     @Override
     38     public UserVo loadDeptRoleService()
     39     {
     40         List<Dept> deptList = deptDao.getAllDepts();
     41         List<Role> roleList = roleDao.getAllRoles();
     42         UserVo userVo = new UserVo();
     43         userVo.setDeptList(deptList);
     44         userVo.setRoleList(roleList);
     45         return userVo;
     46     }
     47 
     48     @Override
     49     public void saveUserService(UserVo userVo)
     50     {
     51         //将userVo转化为pojo对象
     52         User user = new User();
     53         try
     54         {
     55             org.springframework.beans.BeanUtils.copyProperties(userVo, user);
     56         }
     57         catch (Exception e)
     58         {
     59             e.printStackTrace();
     60         }
     61         userDao.saveUser(user);
     62         //通过user建立和部门关系
     63         Integer did = userVo.getDid();
     64         if(!"".equals(did) && did != null)
     65         {
     66             Dept dept = new Dept();
     67             dept.setDid(did);
     68             user.setDept(dept);
     69         }
     70         //通过user建立和角色的关系
     71         Integer rids[] = userVo.getRids();
     72         if(rids != null)
     73         {
     74             for(Integer rid : rids)
     75             {
     76                 Role role = new Role();
     77                 role.setRid(rid);
     78                 user.getRoles().add(role);
     79             }
     80         }
     81     }
     82 
     83     @Override
     84     public List<UserVo> loadUserListService()
     85     {
     86         List<User> userList = userDao.getAllUsers();
     87         //创建UserVoList集合
     88         List<UserVo> userVoList = new ArrayList<UserVo>();
     89         //将数据库查询到的结果封装为需要给用户显示的数据模型
     90         for(User user : userList)
     91         {
     92             //封装用户的角色集合
     93             Set<Role> roleSet = user.getRoles();
     94             StringBuffer rnameBuffer = new StringBuffer(); 
     95             for(Role role : roleSet)
     96             {
     97                 rnameBuffer.append(role.getRname());
     98                 rnameBuffer.append(',');
     99             }
    100             String rname = rnameBuffer.toString();
    101             if(!"".equals(rname))
    102             {
    103                 rname = rname.substring(0,rname.lastIndexOf(','));
    104             }
    105             UserVo userVo = new UserVo();
    106             try
    107             {
    108                 org.springframework.beans.BeanUtils.copyProperties(user, userVo);
    109             }
    110             catch (Exception e)
    111             {
    112                 e.printStackTrace();
    113             }
    114             userVo.setRname(rname);
    115             userVoList.add(userVo);
    116         }
    117         return userVoList;
    118     }
    119 
    120     @Override
    121     public void deleteUserService(UserVo userVo)
    122     {
    123         //将userVo转化为pojo对象
    124         User user = new User();
    125         try
    126         {
    127             org.springframework.beans.BeanUtils.copyProperties(userVo, user);
    128         }
    129         catch (Exception e)
    130         {
    131             e.printStackTrace();
    132         }
    133         user = userDao.getUserById(user);
    134         user.setDept(null);
    135         user.setRoles(null);
    136         userDao.deleteUser(user);
    137     }
    138 
    139     @Override
    140     public void updateUserService(UserVo userVo)
    141     {
    142         //将userVo转化为pojo对象
    143         User user = new User();
    144         try
    145         {
    146             org.springframework.beans.BeanUtils.copyProperties(userVo, user);
    147         }
    148         catch (Exception e)
    149         {
    150             e.printStackTrace();
    151         }
    152         userDao.updateUser(user);
    153         //通过user建立和部门关系
    154         Integer did = userVo.getDid();
    155         if(!"".equals(did) && did != null)
    156         {
    157             Dept dept = new Dept();
    158             dept.setDid(did);
    159             user.setDept(dept);
    160         }
    161         //通过user建立和角色的关系
    162         Integer rids[] = userVo.getRids();
    163         if(rids != null)
    164         {
    165             for(Integer rid : rids)
    166             {
    167                 Role role = new Role();
    168                 role.setRid(rid);
    169                 user.getRoles().add(role);
    170             }
    171         }
    172     }
    173 
    174     @Override
    175     public UserVo getUserByIdService(UserVo userVo)
    176     {
    177         User user = new User();
    178         user.setUid(userVo.getUid());
    179         user = userDao.getUserById(user);
    180         org.springframework.beans.BeanUtils.copyProperties(user, userVo);
    181         return userVo;
    182     }
    183 }

    1.7建立UsersAction

     1 package org.guangsoft.action;
     2 import java.util.List;
     3 
     4 import org.guangsoft.service.UserService;
     5 import org.guangsoft.vo.UserVo;
     6 import org.springframework.beans.BeanUtils;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.context.annotation.Scope;
     9 import org.springframework.stereotype.Controller;
    10 
    11 import com.opensymphony.xwork2.ActionContext;
    12 import com.opensymphony.xwork2.ModelDriven;
    13 
    14 @Controller
    15 @Scope("prototype") 
    16 public class UserAction implements ModelDriven<UserVo>
    17 {
    18     //自动装配service
    19     @Autowired
    20     private UserService userService;
    21     //创建页面对象
    22     private UserVo userVo = new UserVo();
    23     
    24     @Override
    25     public UserVo getModel()
    26     {
    27         return userVo;
    28     }
    29     public UserVo getUserVo()
    30     {
    31         return userVo;
    32     }
    33     public void setUserVo(UserVo userVo)
    34     {
    35         this.userVo = userVo;
    36     }
    37     public UserService getUserService()
    38     {
    39         return userService;
    40     }
    41     public void setUserService(UserService userService)
    42     {
    43         this.userService = userService;
    44     }
    45     
    46      //加载添加用户页面
    47     public String loadSaveUi()
    48     {
    49         UserVo userVoLoad = userService.loadDeptRoleService();
    50         userVo.setDeptList(userVoLoad.getDeptList());
    51         userVo.setRoleList(userVoLoad.getRoleList());
    52         return ActionConstants.TO_SAVE_UI;
    53     }
    54     
    55     //添加用户
    56     public String addUser()
    57     {
    58         userService.saveUserService(userVo);
    59         return ActionConstants.TO_LIST_ACTION;
    60     }
    61     
    62     //加载修改用户页面
    63     public String loadUpdateUi()
    64     {
    65         userVo = userService.getUserByIdService(userVo);
    66         loadSaveUi();
    67         return ActionConstants.TO_UPDATE_UI;
    68     }
    69     
    70     //修改用户
    71     public String updateUser()
    72     {
    73         userService.updateUserService(userVo);
    74         return ActionConstants.TO_LIST_ACTION;
    75     }
    76     
    77     //加载用户列表
    78     public String loadListUi()
    79     {
    80         //获得用户集合
    81         List<UserVo> userVoList = userService.loadUserListService();
    82         ActionContext.getContext().put("userVoList", userVoList);
    83         return ActionConstants.TO_LIST_UI;
    84     }
    85     
    86     //删除用户
    87     public String deleteUser()
    88     {
    89         userService.deleteUserService(userVo);
    90         return ActionConstants.TO_LIST_ACTION;
    91     }
    92     
    93 }

    1.8配置Action

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
     3 <struts>
     4     <package name="user" namespace="/" extends="struts-default">
     5         <!-- action在spring容器中对应的id -->
     6         <action name="userAction_*" class="userAction" method="{1}">
     7             <result name="TO_SAVE_UI">/WEB-INF/jsp/saveUser.jsp</result>
     8             <result name="TO_UPDATE_UI">/WEB-INF/jsp/updateUser.jsp</result>
     9             <result name="TO_LIST_UI">/WEB-INF/jsp/userList.jsp</result>
    10             <result name="TO_LIST_ACTION" type="redirectAction">userAction_loadListUi</result>
    11         </action>
    12     </package>
    13 </struts>    

    1.9建立测试文件

    TestUserDao.java

     1 package org.guangsoft.test;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.dao.UserDao;
     6 import org.guangsoft.pojo.User;
     7 import org.junit.Test;
     8 import org.springframework.context.ApplicationContext;
     9 import org.springframework.context.support.ClassPathXmlApplicationContext;
    10 
    11 public class TestUserDao
    12 {
    13     @Test
    14     public void testSaveUser()
    15     {
    16         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    17         UserDao userDao = (UserDao) ac.getBean("userDaoImpl");
    18         User user = new User();
    19         user.setUsername("testUserDao01");;
    20         userDao.saveUser(user);
    21     }
    22     
    23     @Test
    24     public void testGetAllUsers()
    25     {
    26         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    27         UserDao userDao = (UserDao) ac.getBean("userDaoImpl");
    28         List<User> userList = userDao.getAllUsers();
    29         System.out.println(userList);
    30     }
    31     
    32     @Test
    33     public void testdeleteUser()
    34     {
    35         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    36         UserDao userDao = (UserDao) ac.getBean("userDaoImpl");
    37         User user = new User();
    38         user.setUid(45);
    39         user = userDao.getUserById(user);
    40         userDao.deleteUser(user);
    41     }
    42     
    43     @Test 
    44     public void testUpdateUser()
    45     {
    46         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    47         UserDao userDao = (UserDao) ac.getBean("userDaoImpl");
    48         User user = new User();
    49         user.setUid(46);
    50         user.setUsername("sfdsfsdfdsfds");
    51         user.setDept(null);
    52         userDao.updateUser(user);
    53     }
    54 }

    TestUserService.java

     1 package org.guangsoft.test;
     2 
     3 import java.util.List;
     4 
     5 import org.guangsoft.service.UserService;
     6 import org.guangsoft.vo.UserVo;
     7 import org.junit.Test;
     8 import org.springframework.context.ApplicationContext;
     9 import org.springframework.context.support.ClassPathXmlApplicationContext;
    10 
    11 public class TestUserService
    12 {
    13     @Test 
    14     public void testLoadSaveUiService()
    15     {
    16         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    17         UserService userService = (UserService) ac.getBean("userServiceImpl");
    18         UserVo userVo = userService.loadDeptRoleService();
    19         System.out.println(userVo.getDeptList());
    20         System.out.println(userVo.getRoleList());
    21     }
    22     
    23     @Test
    24     public void testSaveUserService()
    25     {
    26         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    27         UserService userService = (UserService) ac.getBean("userServiceImpl");
    28         UserVo userVo = new UserVo();
    29         Integer rids[] = {1,2,3};
    30         userVo.setRids(rids);
    31         userVo.setDid(1);
    32         userVo.setUsername("test02");
    33         userService.saveUserService(userVo);
    34     }
    35     
    36     @Test 
    37     public void testLoadListUiService()
    38     {
    39         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    40         UserService userService = (UserService) ac.getBean("userServiceImpl");
    41         List<UserVo> userVoList = userService.loadUserListService();
    42         System.out.println(userVoList.get(0).getUsername());
    43     }
    44     
    45     @Test
    46     public void testDeleteUserService()
    47     {
    48         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    49         UserService userService = (UserService) ac.getBean("userServiceImpl");
    50         UserVo userVo = new UserVo();
    51         userVo.setUid(47);
    52         userService.deleteUserService(userVo);
    53     }
    54     
    55     @Test
    56     public void testUpdateUserService()
    57     {
    58         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    59         UserService userService = (UserService) ac.getBean("userServiceImpl");
    60         UserVo userVo = new UserVo();
    61         Integer rids[] = {1,2,3};
    62         userVo.setRids(rids);
    63         userVo.setDid(1);
    64         userVo.setUsername("test02");
    65         userVo.setUid(46);
    66         userService.updateUserService(userVo);
    67     }
    68     
    69     @Test
    70     public void testGetUserByIdService()
    71     {
    72         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    73         UserService userService = (UserService) ac.getBean("userServiceImpl");
    74         UserVo userVo = new UserVo();
    75         userVo.setUid(49);
    76         userVo = userService.getUserByIdService(userVo);
    77         System.out.println(userVo.getUsername());
    78     }
    79 }

    1.10建立辅助文件

    UsersVo.java(封装客户端请求的数据,需要响应到客户端的数据)

     1 package org.guangsoft.vo;
     2 
     3 import java.util.Arrays;
     4 import java.util.List;
     5 
     6 import org.guangsoft.pojo.Dept;
     7 import org.guangsoft.pojo.Role;
     8 import org.guangsoft.pojo.User;
     9 
    10 /**
    11  * 定义User的vo类
    12  * 封装需要给哭护短响应的数据
    13  * 封装接受请求的数据
    14  * @author guanghe
    15  */
    16 public class UserVo extends User
    17 {
    18     //在添加用户的时候需要加载部门的集合
    19     private List<Dept> deptList;
    20     //封装角色的集合
    21     private List<Role> roleList;
    22     //获得角色的id
    23     private Integer rids[];
    24     //获得部门id
    25     private Integer did;
    26     //保存用户角色的名字,展示使用
    27     private String rname;
    28     
    29     public List<Dept> getDeptList()
    30     {
    31         return deptList;
    32     }
    33     public void setDeptList(List<Dept> deptList)
    34     {
    35         this.deptList = deptList;
    36     }
    37     public Integer[] getRids()
    38     {
    39         return rids;
    40     }
    41     public void setRids(Integer[] rids)
    42     {
    43         this.rids = rids;
    44     }
    45     public List<Role> getRoleList()
    46     {
    47         return roleList;
    48     }
    49     public void setRoleList(List<Role> roleList)
    50     {
    51         this.roleList = roleList;
    52     }
    53     public String getRname()
    54     {
    55         return rname;
    56     }
    57     public void setRname(String rname)
    58     {
    59         this.rname = rname;
    60     }
    61     public Integer getDid()
    62     {
    63         return did;
    64     }
    65     public void setDid(Integer did)
    66     {
    67         this.did = did;
    68     }
    69     
    70     
    71 }

    ActionContants(常量池,封装了跳转的常用字符串)

    1 package org.guangsoft.action;
    2 
    3 public interface ActionConstants
    4 {
    5     String TO_SAVE_UI  = "TO_SAVE_UI";
    6     String TO_UPDATE_UI  = "TO_UPDATE_UI";
    7     String TO_LIST_UI = "TO_LIST_UI";
    8     String TO_LIST_ACTION = "TO_LIST_ACTION";
    9 }

    Commons.jsp(将公共的资源文件进行统一管理) 

    1 <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/blue/pageCommon.css" />
    2 <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>

    1.11建立页面

    加载saveUser.jsp,主要显示所有的部门和角色

      1 <%@ page language="java" pageEncoding="UTF-8"%>
      2 <%@ taglib uri="/struts-tags" prefix="s" %>
      3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
      4 <%
      5 String path = request.getContextPath();
      6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
      7 %>
      8 
      9 <!DOCTYPE HTML>
     10 <html>
     11       <head>
     12         <title>添加用户</title>
     13         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     14         <base href="<%=basePath%>">
     15         <%@ include file="commons.jsp"%>
     16         <script src="js/user.js"></script>
     17     </head>
     18     <body>
     19         <div id="Title_bar">
     20             <div id="Title_bar_Head">
     21                 <div id="Title_Head"></div>
     22                 <div id="Title">
     23                     <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/css/images/title_arrow.gif"/> 用户信息
     24                 </div>
     25                 <div id="Title_End"></div>
     26             </div>
     27         </div>
     28         <div id=MainArea>
     29             <s:form action="userAction_addUser.action" id="addUserForm" theme="simple" method="post">
     30                 <div class="ItemBlock_Title1">
     31                     <img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif" /> 用户信息  
     32                 </div>
     33                 <div class="ItemBlockBorder">
     34                     <div class="ItemBlock">
     35                         <table cellpadding="0" cellspacing="0" class="mainForm">
     36                             <tr>
     37                                 <td width="100">所属部门</td>
     38                                 <td>
     39                                     <s:select list="deptList" listKey="did" listValue="dname" 
     40                                         name="did" cssClass="SelectStyle" />
     41                                 </td>
     42                             </tr>
     43                             <tr><td>登录名</td>
     44                                 <td>
     45                                     <s:textfield name="username" cssClass="InputStyle" />*(登录名要唯一)
     46                                 </td>
     47                             </tr>
     48                             <tr>
     49                                 <td>性别</td>
     50                                 <td>
     51                                     <s:radio list="#{'男':'男','女':'女'}" name="sex"></s:radio>
     52                                 </td>
     53                             </tr>
     54                             <tr>
     55                                 <td>联系电话</td>
     56                                 <td>
     57                                     <s:textfield name="phone" cssClass="InputStyle"/>
     58                                 </td>
     59                             </tr>
     60                             <tr>
     61                                 <td>E-mail</td>
     62                                 <td>
     63                                     <s:textfield name="email" cssClass="InputStyle"></s:textfield>
     64                                 </td>
     65                             </tr>
     66                         </table>
     67                     </div>
     68                 </div>
     69                 
     70                 <div class="ItemBlock_Title1">
     71                     <img border="0" width="4" height="7" src="${pageContext.request.contextPath}/css/blue/images/item_point.gif" /> 岗位设置 
     72                 </div>
     73                 
     74                 <!-- 表单内容显示 -->
     75                 <div class="ItemBlockBorder">
     76                     <div class="ItemBlock">
     77                         <table cellpadding="0" cellspacing="0" class="mainForm">
     78                             <tr>
     79                                 <td width="100">岗位</td>
     80                                 <td>
     81                                     <s:select list="roleList" listKey="rid" listValue="rname"
     82                                         name="rids" cssClass="SelectStyle" multiple="true" />
     83                                     按住Ctrl键可以多选或取消选择
     84                                 </td>
     85                             </tr>
     86                         </table>
     87                     </div>
     88                 </div>        
     89                 
     90                 <!-- 表单操作 -->
     91                 <div id="InputDetailBar">
     92                     <input type="image" onclick="addUser();" src="${pageContext.request.contextPath}/css/images/save.png"/>
     93                     <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/css/images/goBack.png"/></a>
     94                 </div>
     95             </s:form>
     96         </div>
     97         
     98         <div class="Description">
     99             说明:<br />
    100             1,用户的登录名要唯一,在填写时要同时检测是否可用。<br />
    101             2,新建用户后,密码被初始化为"1234"。<br />
    102             3,密码在数据库中存储的是MD5摘要(不是存储明文密码)。<br />
    103             4,用户登录系统后可以使用“个人设置→修改密码”功能修改密码。<br />
    104             5,新建用户后,会自动指定默认的头像。用户可以使用“个人设置→个人信息”功能修改自已的头像<br />
    105             6,修改用户信息时,登录名不可修改。
    106         </div>
    107     </body>
    108 </html>

    加载userList.jsp

     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 <%@ taglib uri="/struts-tags" prefix="s" %>
     3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
     4 <%
     5     String path = request.getContextPath();
     6     String basePath = request.getScheme() + "://"
     7         + request.getServerName() + ":" + request.getServerPort() + path + "/";
     8 %>
     9 
    10 <!DOCTYPE HTML>
    11 <html>
    12     <head>
    13         <base href="<%=basePath%>">
    14         <title>用户列表</title>
    15         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    16         <%@ include file="commons.jsp"%>
    17     </head>
    18     
    19     <body>
    20         <div id="Title_bar">
    21             <div id="Title_bar_Head">
    22                 <div id="Title_Head"></div>
    23                     <div id="Title">
    24                         <img border="0" width="13" height="13"
    25                             src="${pageContext.request.contextPath}/css/images/title_arrow.gif" /> 用户管理
    26                     </div>
    27                 <div id="Title_End"></div>
    28             </div>
    29         </div>
    30         <div id="MainArea">
    31             <table cellspacing="0" cellpadding="0" class="TableStyle">
    32                 <thead>
    33                     <tr align=center valign=middle id=TableTitle>
    34                         <td width="100">用户名</td>
    35                         <td width="100">电话</td>
    36                         <td width="100">所属部门</td>
    37                         <td>角色</td>
    38                         <td>相关操作</td>
    39                     </tr>
    40                 </thead>
    41                 <tbody id="TableData" class="dataContainer" datakey="userList">
    42                     <s:iterator value="userVoList" id="userVo">                
    43                         <tr class="TableDetail1 template">
    44                             <td><s:property value="#userVo.username" />&nbsp;</td>
    45                             <td><s:property value="#userVo.phone" />&nbsp;</td>
    46                             <td><s:property value="#userVo.dept.dname" />&nbsp;</td>
    47                             <td><s:property value="#userVo.rname" />&nbsp;</td>
    48                             <td>
    49                                 <s:a href="userAction_loadUpdateUi.action?uid=%{#userVo.uid}">修改</s:a>&nbsp;&nbsp;
    50                                 <s:a href="userAction_deleteUser.action?uid=%{#userVo.uid}" >删除</s:a>&nbsp;&nbsp;
    51                             </td>
    52                         </tr>
    53                     </s:iterator>
    54                 </tbody>
    55             </table>
    56             <div id="TableTail">
    57                 <div id="TableTail_inside">
    58                     <a href="userAction_loadSaveUi.action"><img src="${pageContext.request.contextPath}/css/images/createNew.png" /></a>
    59                 </div>
    60             </div>
    61         </div>
    62     </body>
    63 </html>
  • 相关阅读:
    字符串个数的麻烦
    最长单调递增子序列LIS(《算法导论》15.4-5题)
    LCS问题
    关于nextLine()与nextInt()
    调用内部类里,在静态类中调用动态方法的问题
    RTL底层释放或回收对象
    软件需求分、架构设计与建模最佳实践
    Spring@Autowired java.lang.NullPointerException 空指针
    MAC下安装REDIS和REDIS可视化工具RDM并连接REDIS
    Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.catalina.connector.CoyoteWriter and no properties discovered to create BeanSerializer
  • 原文地址:https://www.cnblogs.com/guanghe/p/6139134.html
Copyright © 2020-2023  润新知