码迷,mamicode.com
首页 > 其他好文 > 详细

OA项目12:系统管理之用户管理

时间:2014-10-28 19:36:34      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   os   ar   使用   

首注:本学习教程为传智播客汤阳光讲师所公布的免费OA项目视频我的文字版实践笔记,本人用此来加强巩固自己开发知识,如有网友转载,请注明。谢谢。 

一 之前在第8节时已经将User实体及映射文件建立好了,所以设计实体已经完成了,下面来做功能了。

二 我们对照静态页面看用户管理页面分析发现,增删改查功能跟之前一样,多了一个初始化密码的请求。当然,这不是问题,下面开始做功能吧!

三 开发:

  1.写action及struts2映射,具体如下:

    1)UserAction.java:    

    bubuko.com,布布扣
  1 package cn.clear.oa.view.action;
  2 
  3 import java.util.HashSet;
  4 import java.util.List;
  5 
  6 import org.springframework.context.annotation.Scope;
  7 import org.springframework.stereotype.Controller;
  8 
  9 import com.opensymphony.xwork2.ActionContext;
 10 
 11 import cn.clear.oa.base.BaseAction;
 12 import cn.clear.oa.domain.Department;
 13 import cn.clear.oa.domain.Role;
 14 import cn.clear.oa.domain.User;
 15 import cn.clear.oa.util.DepartmentUtil;
 16 
 17 @Controller
 18 @Scope("prototype")
 19 public class UserAction extends BaseAction<User> {
 20 
 21     /**
 22      * 
 23      */
 24     private static final long serialVersionUID = 1L;
 25 
 26     private Long departmentId;
 27     private Long[] roleIds;
 28 
 29     public String list() throws Exception {
 30         // 查询所有的用户
 31         List<User> userList = userService.findAll();
 32         // 将用户放集合
 33         ActionContext.getContext().put("userList", userList);
 34         return "list";
 35     }
 36 
 37     public String delete() throws Exception {
 38         userService.delete(model.getId());
 39         return "toList";
 40     }
 41 
 42     public String addUI() throws Exception {
 43         // 准备部门树状列表
 44         List<Department> topList = departmentService.findTopList();
 45         List<Department> departmentList = DepartmentUtil
 46                 .getAllDepartments(topList);
 47 
 48         ActionContext.getContext().put("departmentList", departmentList);
 49         // 准备roleList
 50         List<Role> roleList = roleService.findAll();
 51         ActionContext.getContext().put("roleList", roleList);
 52         return "saveUI";
 53     }
 54 
 55     public String add() throws Exception {
 56         // 设置部门
 57         model.setDepartment(departmentService.findById(departmentId));
 58         // 设置岗位
 59         model.setRoles(new HashSet<Role>(roleService.findByIds(roleIds)));
 60         // 设置密码
 61         model.setPassword("1234");
 62         userService.save(model);
 63         return "toList";
 64     }
 65 
 66     public String editUI() throws Exception {
 67         // 准备部门树状列表
 68         List<Department> topList = departmentService.findTopList();
 69         List<Department> departmentList = DepartmentUtil
 70                 .getAllDepartments(topList);
 71 
 72         ActionContext.getContext().put("departmentList", departmentList);
 73         // 准备roleList
 74         List<Role> roleList = roleService.findAll();
 75         ActionContext.getContext().put("roleList", roleList);
 76 
 77         // 准备回显的数据
 78         User user = userService.findById(model.getId());
 79         // 放在栈顶
 80         ActionContext.getContext().getValueStack().push(user);
 81         //部门回显
 82         if(user.getDepartment()!=null){
 83             departmentId = user.getDepartment().getId();
 84         }
 85         //岗位回显
 86         if(user.getRoles()!=null){
 87             roleIds = new Long[user.getRoles().size()];
 88             int index = 0;
 89             for (Role role : user.getRoles()) {
 90                 roleIds[index++] = role.getId();
 91             }
 92         }
 93         return "saveUI";
 94     }
 95 
 96     public String edit() throws Exception {
 97         User user = userService.findById(model.getId());
 98         user.setDescription(model.getDescription());
 99         user.setEmail(model.getEmail());
100         user.setGender(model.getGender());
101         user.setLoginName(model.getLoginName());
102         user.setName(model.getName());
103         user.setPhoneNumber(model.getPhoneNumber());
104         
105         List<Role> roleList = roleService.findByIds(roleIds);
106         user.setRoles(new HashSet<Role>(roleList));
107         user.setDepartment(departmentService.findById(departmentId));
108         userService.update(user);
109         return "toList";
110     }
111 
112     public String initPassword() throws Exception {
113         User user = userService.findById(model.getId());
114         user.setPassword("1234");
115         userService.update(user);
116         return "toList";
117     }
118 
119     // ------
120     public Long getDepartmentId() {
121         return departmentId;
122     }
123 
124     public void setDepartmentId(Long departmentId) {
125         this.departmentId = departmentId;
126     }
127 
128     public Long[] getRoleIds() {
129         return roleIds;
130     }
131 
132     public void setRoleIds(Long[] roleIds) {
133         this.roleIds = roleIds;
134     }
135 
136 }
UserAction.java

    2)在struts.xml中添加映射:  

    bubuko.com,布布扣
1 <!-- 用户管理 -->
2         <action name="user_*" class="userAction" method="{1}">
3             <result name="list">/WEB-INF/jsp/userAction/list.jsp</result>
4             <result name="toList" type="redirectAction">user_list</result>
5             <result name="saveUI">/WEB-INF/jsp/userAction/saveUI.jsp</result>
6             
7         </action>
在struts.xml中添加action

  2.写Service及实现类:

    1)UserService.java   

    bubuko.com,布布扣
1 package cn.clear.oa.service;
2 
3 import cn.clear.oa.base.DaoSupport;
4 import cn.clear.oa.domain.User;
5 
6 public interface UserService extends DaoSupport<User>{
7 
8 }
UserService.java

    2)UserServiceImpl.java    

    bubuko.com,布布扣
 1 package cn.clear.oa.service.impl;
 2 
 3 import org.springframework.stereotype.Service;
 4 
 5 import cn.clear.oa.base.DaoSupportImpl;
 6 import cn.clear.oa.domain.User;
 7 import cn.clear.oa.service.UserService;
 8 
 9 @Service
10 public class UserServiceImpl extends DaoSupportImpl<User> implements UserService{
11     
12 }
UserServiceImpl.java

    3)在BaseAction.java中声明userService,如下:

    @Resource

    protected UserService userService;

  3.写jsp页面:

    1)list.jsp:  

    bubuko.com,布布扣
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
 2 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5 <head>
 6     <title>用户列表</title>
 7     <%@ include file="/WEB-INF/jsp/public/commons.jspf" %>
 8 </head>
 9 <body>
10 
11 <div id="Title_bar">
12     <div id="Title_bar_Head"> 
13         <div id="Title_Head"></div>
14         <div id="Title"><!--页面标题-->
15             <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 用户管理
16         </div>
17         <div id="Title_End"></div>
18     </div>
19 </div>
20 
21 <div id="MainArea">
22     <table cellspacing="0" cellpadding="0" class="TableStyle">
23        
24         <!-- 表头-->
25         <thead>
26             <tr align=center valign=middle id=TableTitle>
27                 <td width="100">登录名</td>
28                 <td width="100">姓名</td>
29                 <td width="100">所属部门</td>
30                 <td width="200">岗位</td>
31                 <td>备注</td>
32                 <td>相关操作</td>
33             </tr>
34         </thead>
35         
36         <!--显示数据列表-->
37         <tbody id="TableData" class="dataContainer" datakey="userList">
38         <s:iterator value="#userList">
39             <tr class="TableDetail1 template">
40                 <td>${loginName}&nbsp;</td>
41                 <td>${name}&nbsp;</td>
42                 <td>${department.name}&nbsp;</td>
43                 <td>
44                     <s:iterator value="#roles">
45                         ${name} 
46                     </s:iterator>
47                     </td>
48                 <td>${description}&nbsp;</td>
49                 <td><s:a onClick="return confirm(‘确定删除该用户吗?‘)" action="user_delete?id=%{id}">删除</s:a>
50                     <s:a action="user_editUI?id=%{id}">修改</s:a>
51                     <s:a action="user_initPassword?id=%{id}" onClick="return confirm(‘您确定要初始化密码为1234吗?‘)">初始化密码</s:a>
52                 </td>
53             </tr>
54         </s:iterator>    
55         </tbody>
56     </table>
57     
58     <!-- 其他功能超链接 -->
59     <div id="TableTail">
60         <div id="TableTail_inside">
61             <s:a action="user_addUI"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></s:a>
62         </div>
63     </div>
64 </div>
65 
66 </body>
67 </html>
list.jsp

    2)saveUI.jsp:

    bubuko.com,布布扣
  1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2 
  3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4 
  5 <html>
  6 <head>
  7     <title>用户信息</title>
  8     <%@ include file="/WEB-INF/jsp/public/commons.jspf" %>
  9 </head>
 10 <body>
 11 
 12 <!-- 标题显示 -->
 13 <div id="Title_bar">
 14     <div id="Title_bar_Head">
 15         <div id="Title_Head"></div>
 16         <div id="Title"><!--页面标题-->
 17             <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 用户信息
 18         </div>
 19         <div id="Title_End"></div>
 20     </div>
 21 </div>
 22 
 23 <!--显示表单内容-->
 24 <div id=MainArea>
 25     <s:form action="user_%{id == null ? ‘add‘:‘edit‘ }">
 26         <div class="ItemBlock_Title1"><!-- 信息说明 --><div class="ItemBlock_Title1">
 27             <img border="0" width="4" height="7" src="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 用户信息 </div> 
 28         </div>
 29         <!-- 表单内容显示 -->
 30         <s:hidden name="id"></s:hidden>
 31         <div class="ItemBlockBorder">
 32             <div class="ItemBlock">
 33                 <table cellpadding="0" cellspacing="0" class="mainForm">
 34                     <tr><td width="100">所属部门</td>
 35                         <td><s:select name="departmentId" list="#departmentList" cssClass="SelectStyle" listKey="id" listValue="name"
 36                         headerKey="" headerValue="==请选择部门=="></s:select>
 37                         </td>
 38                     </tr>
 39                     <tr><td>登录名</td>
 40                         <td><s:textfield name="loginName" cssClass="InputStyle"></s:textfield> *
 41                             (登录名要唯一)
 42                         </td>
 43                     </tr>
 44                     <tr><td>姓名</td>
 45                         <td><s:textfield name="name" cssClass="InputStyle"/> *</td>
 46                     </tr>
 47                     <tr><td>性别</td>
 48                         <td>
 49                             <s:radio name="gender" list="{‘男‘,‘女‘}">
 50                             </s:radio>
 51                         </td>
 52                     </tr>
 53                     <tr><td>联系电话</td>
 54                         <td><s:textfield name="phoneNumber" cssClass="InputStyle"/></td>
 55                     </tr>
 56                     <tr><td>E-mail</td>
 57                         <td><s:textfield name="email" cssClass="InputStyle"/></td>
 58                     </tr>
 59                     <tr><td>备注</td>
 60                         <td><s:textarea name="description" cssClass="TextareaStyle"></s:textarea></td>
 61                     </tr>
 62                 </table>
 63             </div>
 64         </div>
 65         
 66         <div class="ItemBlock_Title1"><!-- 信息说明 --><div class="ItemBlock_Title1">
 67             <img border="0" width="4" height="7" src="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 岗位设置 </div> 
 68         </div>
 69         
 70         <!-- 表单内容显示 -->
 71         <div class="ItemBlockBorder">
 72             <div class="ItemBlock">
 73                 <table cellpadding="0" cellspacing="0" class="mainForm">
 74                     <tr>
 75                         <td width="100">岗位</td>
 76                         <td><s:select name="roleIds" multiple="true" size="10"
 77                          list="#roleList" cssClass="SelectStyle" listKey="id" listValue="name"></s:select>
 78                             按住Ctrl键可以多选或取消选择
 79                         </td>
 80                     </tr>
 81                 </table>
 82             </div>
 83         </div>        
 84         
 85         <!-- 表单操作 -->
 86         <div id="InputDetailBar">
 87             <input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/>
 88             <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a>
 89         </div>
 90     </s:form>
 91 </div>
 92 
 93 <div class="Description">
 94     说明:<br />
 95     1,用户的登录名要唯一,在填写时要同时检测是否可用。<br />
 96     2,新建用户后,密码被初始化为"1234"。<br />
 97     3,密码在数据库中存储的是MD5摘要(不是存储明文密码)。<br />
 98     4,用户登录系统后可以使用“个人设置→修改密码”功能修改密码。<br />
 99     5,新建用户后,会自动指定默认的头像。用户可以使用“个人设置→个人信息”功能修改自已的头像<br />
100     6,修改用户信息时,登录名不可修改。
101 </div>
102 
103 </body>
104 </html>
saveUI.jsp

    3)在测试时发现roles显示有问题,经检查在外面之前写代码时发现问题,问题是在DaoSupportImpl.java中findByIds()方法这句:

    return getSession().createQuery(//

    "FROM "+clazz.getSimpleName()+" WHERE id IN(:ids)")//

    .setParameter("ids", ids)//

    .list()setParameter有误,因为是集合所以修改后为:

    return getSession().createQuery(//

    "FROM "+clazz.getSimpleName()+" WHERE id IN(:ids)")//

    .setParameterList("ids", ids)//

    .list()

以上用户管理的基本实现,关于在saveUI页面下角的要求将在下节实现。

OA项目12:系统管理之用户管理

标签:des   style   blog   http   io   color   os   ar   使用   

原文地址:http://www.cnblogs.com/clear5/p/4057356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!