管理员——用户管理

查询所有用户

  1. 添加用户管理树枝
d.add('0106','01','用户管理');
	d.add('010601','0106','会员用户','${pageContext.request.contextPath}/adminUserServlet?method=findAllUsers&num=1&state=1','','mainFrame');
    d.add('010602','0106','未激活用户','${pageContext.request.contextPath}/adminUserServlet?method=findAllUsers&num=1&state=0','','mainFrame');
  1. 获取所有用户
    根据state来查询普通用户和未激活用户并分页
//获取所有用户
public String findAllUsers(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    
    
    //获取当前页
    int curNum = Integer.parseInt(req.getParameter("num"));
    int state = Integer.parseInt(req.getParameter("state"));
    //调用业务层查全部用户信息返回PageModel
    UserService userService = new UserServiceImp();
    PageModel pm = userService.findAllUsers(state, curNum);
    //将PageModel放入request
    req.setAttribute("page", pm);
    //转发到/admin/product/list.jsp
    return "/admin/user/list.jsp";
}
  1. 业务层
public PageModel findAllUsers(int state,int curNum) throws SQLException {
    
    
    //1_创建对象
    int totalRecords = userDao.findTotalRecords(state);
    PageModel pm = new PageModel(curNum, totalRecords, 5);
    //2_关联集合 select * from user where state=? product limit ? , ?
    List<User> list = userDao.findAllUsers(state, pm.getStartIndex(), pm.getPageSize());
    pm.setList(list);
    //3_关联url
    pm.setUrl("adminUserServlet?method=findAllUsers&state="+state);
    return pm;
}
  1. dao层
public int findTotalRecords(int state) throws SQLException {
    
    
    String sql = "select count(*) from user where state=?";
    return template.queryForObject(sql, Integer.class, state);
}
public List<User> findAllUsers(int state,int startIndex, int pageSize) throws SQLException {
    
    
    String sql = "select * from user where state =? limit  ? , ?";
    return template.query(sql, new BeanPropertyRowMapper<>(User.class),state,startIndex, pageSize);
}
  1. 编写"/admin/user/list.jsp"
<form id="Form1" name="Form1" action="${pageContext.request.contextPath}/admin/user/list.jsp" method="post">
	<table cellSpacing="1" cellPadding="0" width="100%" align="center" bgColor="#f5fafe" border="0">
		<TBODY>
		<tr>
			<td class="ta_01" align="center" bgColor="#afd1f3">
				<strong>用户列表</strong>
			</TD>
		</tr>
		<tr>

		</tr>
		<tr>
			<td class="ta_01" align="center" bgColor="#f5fafe">
				<table cellspacing="0" cellpadding="1" rules="all"
					   bordercolor="gray" border="1" id="DataGrid1"
					   style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
					<tr
							style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">

						<td align="center" width="18%">
							序号
						</td>
						<td align="center" width="17%">
							用户名称
						</td>
						<td align="center" width="17%">
							真实姓名
						</td>
						<td width="7%" align="center">
							编辑
						</td>
						<td width="7%" align="center">
							删除
						</td>
					</tr>
					<c:forEach items="${page.list}" var="u" varStatus="status">
						<tr onmouseover="this.style.backgroundColor = 'white'"
							onmouseout="this.style.backgroundColor = '#F5FAFE';">
							<td style="CURSOR: hand; HEIGHT: 22px" align="center"
								width="18%">
								${
    
    status.count}
							</td>
							<td style="CURSOR: hand; HEIGHT: 22px" align="center"
								width="17%">
									${
    
    u.username}
							</td>
							<td style="CURSOR: hand; HEIGHT: 22px" align="center"
								width="17%">
									${
    
    u.name}
							</td>
							<td align="center" style="HEIGHT: 22px">
								<a href="${ pageContext.request.contextPath }/adminUserServlet?method=editUserUI&uid=${u.uid}&username=${u.username}&password=${u.password}&name=${u.name}&sex=${u.sex}&email=${u.email}&telephone=${u.telephone}&state=${u.state}">
									<img src="${pageContext.request.contextPath}/img/admin/i_edit.gif" border="0" style="CURSOR: hand">
								</a>
							</td>

							<td align="center" style="HEIGHT: 22px">
								<a href="${ pageContext.request.contextPath }/adminUserServlet?method=delUser&uid=${u.uid}&state=${u.state}">
									<img src="${pageContext.request.contextPath}/img/admin/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
								</a>
							</td>
						</tr>
					</c:forEach>
				</table>
			</td>
		</tr>

		</TBODY>
	</table>
	<%@include file="/jsp/pageFile.jsp" %>
</form>

编辑用户

  1. 给编辑图标添加链接
    传递用户各个属性参数
<td align="center" style="HEIGHT: 22px">
    <a href="${ pageContext.request.contextPath }/adminUserServlet?method=editUserUI&uid=${u.uid}&username=${u.username}&password=${u.password}&name=${u.name}&sex=${u.sex}&email=${u.email}&telephone=${u.telephone}&state=${u.state}">
        <img src="${pageContext.request.contextPath}/img/admin/i_edit.gif" border="0" style="CURSOR: hand">
    </a>
</td>
  1. 转到编辑页面
public String editUserUI(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    
    
    User user = new User();
    MyBeanUtils.populate(user, req.getParameterMap());
    req.setAttribute("u",user);
    return "/admin/user/edit.jsp" ;
}
  1. 编写编辑页面/admin/user/edit.jsp
    用户属性参数作为编辑默认值
<form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/adminUserServlet?method=editUser&state=${u.state}" method="post" >
    <input type="hidden" name="uid" value="${u.uid}"/>
    <input type="hidden" name="state" value="${u.state}" />
    <input type="hidden" name="code" value="${u.code}" />
    &nbsp;
    <table cellSpacing="1" cellPadding="5" width="100%" align="center" bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
        <tr>
            <td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4"
                height="26">
                <strong><STRONG>编辑用户</STRONG>
                </strong>
            </td>
        </tr>

        <tr>
            <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                用户名称:
            </td>
            <td class="ta_01" bgColor="#ffffff">
                <input type="text" name="username" value="${u.username}" id="username" class="bg"/>
            </td>
            <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                密码:
            </td>
            <td class="ta_01" bgColor="#ffffff">
                <input type="text" name="password" value="${u.password}" id="password" class="bg"/>
            </td>
        </tr>
        <tr>
            <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                真实姓名:
            </td>
            <td class="ta_01" bgColor="#ffffff">
                <input type="text" name="name" value="${u.name}" id="name" class="bg"/>
                </td>
            <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                性别:
            </td>
            <td class="ta_01" bgColor="#ffffff">
                <input type="text" name="sex" value="${u.sex}" id="sex" class="bg"/>
            </td>
        </tr>
        <tr>
            <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                邮箱:
            </td>
            <td class="ta_01" bgColor="#ffffff">
                <input type="text" name="email" value="${u.email}" id="email" class="bg"/>
                </td>
            <td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
                电话:
            </td>
            <td class="ta_01" bgColor="#ffffff">
                <input type="text" name="telephone" value="${u.telephone}" id="telephone" class="bg"/>
            </td>
        </tr>
    
        <tr>
            <td class="ta_01" style="WIDTH: 100%" align="center"
                bgColor="#f5fafe" colSpan="4">
                <button type="submit" id="userAction_save_do_submit" value="确定" class="button_ok">
                    &#30830;&#23450;
                </button>

                <span style="font-family: 宋体; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                <button type="reset" value="重置" class="button_cancel">&#37325;&#32622;</button>

                <span style="font-family: 宋体; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                <input class="button_ok" type="button" onclick="history.go(-1)" value="返回"/>
                <span id="Label1"></span>
            </td>
        </tr>
    </table>
</form>
  1. 编写editUser方法
public String editUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    
    
    User user = new User();
    MyBeanUtils.populate(user, req.getParameterMap());
    UserService userService = new UserServiceImp();
    userService.editUser(user);
    System.out.println(user);
    //重定向到查询全部用户信息
    resp.sendRedirect(  req.getContextPath()+"/adminUserServlet?method=findAllUsers&num=1&state="+user.getState());
    return null;
}
  1. 业务层
public void editUser(User user) throws SQLException {
    
    
    userDao.editUser(user);
}
  1. dao层
public void editUser(User user) throws SQLException {
    
    
    String sql = "update user set username=?,password=?,name=?,email=?,telephone=?,sex=? where uid=?";
    Object[] params = {
    
    user.getUsername(), user.getPassword(), user.getName(), user.getEmail(), user.getTelephone(), user.getSex(),user.getUid()};
    template.update(sql, params);
}

删除用户

  1. 给删除图标添加链接
<td align="center" style="HEIGHT: 22px">
    <a href="${ pageContext.request.contextPath }/adminUserServlet?method=delUser&uid=${u.uid}&state=${u.state}">
        <img src="${pageContext.request.contextPath}/img/admin/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
    </a>
</td>
  1. 删除用户返回列表
public String delUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    
    
    String uid = req.getParameter("uid");
    int state = Integer.parseInt(req.getParameter("state"));
    UserService userService = new UserServiceImp();
    userService .delUser(uid);
    resp.sendRedirect(  req.getContextPath()+"/adminUserServlet?method=findAllUsers&num=1&state="+state);
    return null;
}
  1. 业务层
public void delUser(String uid) throws SQLException {
    
    
    userDao.delUser(uid);
}
  1. dao层
public void delUser(String uid) throws SQLException {
    
    
    String sql = "delete from user WHERE uid =?";
    template.update(sql, uid);
}

猜你喜欢

转载自blog.csdn.net/qq_40857365/article/details/111303389