用户管理页面
-
用户管理页面的展示
以上可见:当前页面需要从数据中查询的数据有,用户角色的下拉列表,所有的用户信息,分页信息,这是三个不同的查询; -
具体的实现
//写好的分页工具类 //封装:属性私有,向外提供set,get方法,在set方法中可以做一些安全性的判断。 public class PageSupport { //当前页码-来自于用户输入 private int currentPageNo = 1; //总数量(表) private int totalCount = 0; //页面容量 private int pageSize = 0; //总页数-totalCount/pageSize(+1) private int totalPageCount = 1; public int getCurrentPageNo() { return currentPageNo; } public void setCurrentPageNo(int currentPageNo) { if(currentPageNo > 0){ this.currentPageNo = currentPageNo; } } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { if(totalCount > 0){ this.totalCount = totalCount; //设置总页数 this.setTotalPageCountByRs(); } } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { if(pageSize > 0){ this.pageSize = pageSize; } } public int getTotalPageCount() { return totalPageCount; } public void setTotalPageCount(int totalPageCount) { this.totalPageCount = totalPageCount; } public void setTotalPageCountByRs(){ if(this.totalCount % this.pageSize == 0){ this.totalPageCount = this.totalCount / this.pageSize; }else if(this.totalCount % this.pageSize > 0){ this.totalPageCount = this.totalCount / this.pageSize + 1; }else{ this.totalPageCount = 0; } } } //dao层 //查询所有用户时,由于是用户自己选择的条件,所以得拼接动态sql //根据用户名或用户角色查询用户总数 public int getUserCount(Connection connection, String userName, int userRole) throws Exception { PreparedStatement pstm = null; ResultSet rs = null; int count = 0; if(connection != null){ //用StringBuffer是因为userName和userRole可能有也可能没有(也就是sql语句不确定,可变-->动态sql),利用StringBuffer的可变性进行随时添加 StringBuffer sql = new StringBuffer(); //使用count(*)与count(1)的区别 sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id"); //用ArrayList也是因为应对userName和userRole有没有的情况,随时添加。 // 如果直接用object[]={userName,userRole}的话,userName和userRole不一定谁有谁没有,这样在dao层的执行语句中给sql语句的占位符(?)复制时,可能参数对不上,使用ArrayList随时添加可以解决这个问题。 List<Object> list = new ArrayList<Object>(); if(!StringUtils.isNullOrEmpty(userName)){ sql.append(" and u.userName like ?"); list.add("%"+userName+"%"); } if(userRole > 0){ sql.append(" and u.userRole = ?"); list.add(userRole); } //把list转换成数组。 //经过判断,userName和userRole谁存在谁不存在已经确定,list中保存了存在的参数,此时可以转换成数组Object[] params,供BaseDao.execute()调用。 Object[] params = list.toArray(); System.out.println("sql ----> " + sql.toString()); rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params); if(rs.next()){ count = rs.getInt("count");//从结果集中获取最终的数量。 } BaseDao.closeResource(null, pstm, rs); } return count; } //service层 public int getUserCount(String queryUserName, int queryUserRole) { Connection connection = null; int count = 0; try { connection = BaseDao.getConnection(); count = userDao.getUserCount(connection, queryUserName,queryUserRole); } catch (Exception e) { e.printStackTrace(); }finally{ BaseDao.closeResource(connection, null, null); } return count; } //获取角色列表也是一样的逻辑 //servlet的编写比较复杂,要湖区用户数据,根据参数值判断执行情况,进行分页,想用户展示列表,返回前端页面 //重点、难点 private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //查询用户列表 //从前端获取数据 String queryUserName = request.getParameter("queryname"); String temp = request.getParameter("queryUserRole"); String pageIndex = request.getParameter("pageIndex"); int queryUserRole = 0; UserService userService = new UserServiceImpl(); List<User> userList = null; //设置页面容量。 //第一次走这个请求,一定是第一页,页面大小固定 int pageSize = Constant.pageSize; //当前页码 int currentPageNo = 1; //判断这些请求是否要处理 if(queryUserName == null){ queryUserName = ""; } if(temp != null && !temp.equals("")){ queryUserRole = Integer.parseInt(temp); } if(pageIndex != null){ try{ currentPageNo = Integer.valueOf(pageIndex); }catch(NumberFormatException e){ response.sendRedirect("error.jsp"); } } //为了实现分页,需要计算出当前页面和总页面,页面大小... //总数量(表) int totalCount = userService.getUserCount(queryUserName,queryUserRole); //总页数 PageSupport pages=new PageSupport(); pages.setCurrentPageNo(currentPageNo); pages.setPageSize(pageSize); pages.setTotalCount(totalCount); int totalPageCount = pages.getTotalPageCount(); //控制首页和尾页 if(currentPageNo < 1){ currentPageNo = 1; }else if(currentPageNo > totalPageCount){ currentPageNo = totalPageCount; } //获取用户列表展示 userList = userService.getUserList(queryUserName,queryUserRole,currentPageNo, pageSize); request.setAttribute("userList", userList); List<Role> roleList = null; RoleService roleService = new RoleServiceImpl(); roleList = roleService.getRoleList(); request.setAttribute("roleList", roleList); request.setAttribute("queryUserName", queryUserName); request.setAttribute("queryUserRole", queryUserRole); request.setAttribute("totalPageCount", totalPageCount); request.setAttribute("totalCount", totalCount); request.setAttribute("currentPageNo", currentPageNo); request.getRequestDispatcher("userlist.jsp").forward(request, response); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)