1.1 分页类Page
package com.imooc.oa.entity; import java.util.List; public class Page<T> { private int currentPage; //当前页数 private int totalPage; //总页数 private int pageSize; //每页的记录数 private int totalCount; //总的记录数 private List<T> list; public Page(){ } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } public Page(int currentPage, int totalPage, int pageSize, int totalCount, List<T> list) { this.currentPage = currentPage; this.totalPage = totalPage; this.pageSize = pageSize; this.totalCount = totalCount; this.list = list; } @Override public String toString() { return "Page{" + "currentPage=" + currentPage + ", totalPage=" + totalPage + ", pageSize=" + pageSize + ", totalCount=" + totalCount + ", list=" + list + '}'; } }1.2 持久化层Dao层
@Select("<script> select * from employee <if test='start!=null and size!=null'> limit #{start},#{size} </if> </script>") List<Employee> findByPage(HashMap<String,Object> map); @Select("select count(*) from employee") //查询总记录数 int findAllCount();1.3 业务层service层
public interface EmployeeBiz { void add(Employee employee); void edit(Employee employee); void remove(String sn); Employee get(String sn); List<Employee> getAll(); //查询所有数据记录数 int findAllCount(); Page<Employee> findByPage(int currentPage); }1.4 service层实现类
@Override public int findAllCount() { return employeeDao.findAllCount(); } @Override public Page<Employee> findByPage(int currentPage) { HashMap<String, Object> map = new HashMap<>(); Page<Employee> page = new Page<>(); //封装当前的页数 page.setCurrentPage(currentPage); //每页显示的数据 int pageSize = 5; page.setPageSize(pageSize); //封装总记录数 int totalCount = employeeDao.findAllCount(); page.setTotalCount(totalCount); //封装总页数 double totalPage = totalCount; //向上取整 Double number = Math.ceil(totalPage / pageSize); page.setTotalPage(number.intValue()); map.put("start",(currentPage-1)*pageSize); map.put("size",page.getPageSize()); //封装每页显示的数据 List<Employee> lists = employeeDao.findByPage(map); page.setList(lists); return page; }1.5 控制器
@RequestMapping("list") public String list(@RequestParam(value = "currentPage",defaultValue = "1",required = false)int currentPage, Model model){ model.addAttribute("pagemsg",employeeBiz.findByPage(currentPage)); return "employee_list"; }1.6 jsp页面核心代码
<table id="message-table" class="table admin-form theme-warning tc-checkbox-1"> <thead> <tr class=""> <th class="text-center hidden-xs">Select</th> <th class="hidden-xs">工号</th> <th class="hidden-xs">姓名</th> <th class="hidden-xs">所属部门</th> <th class="hidden-xs">职务</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${requestScope.pagemsg.list}" var="emp"> <tr class="message-unread"> <td class="hidden-xs"> <label class="option block mn"> <input type="checkbox" name="mobileos" value="FR"> <span class="checkbox mn"></span> </label> </td> <td>${emp.sn}</td> <td>${emp.name}</td> <td class="text-center fw600">${emp.department.name}</td> <td class="hidden-xs"> <span class="badge badge-warning mr10 fs11">${emp.post}</span> </td> <td> <a href="/employee/to_update?sn=${emp.sn}">编辑</a> <a href="/employee/remove?sn=${emp.sn}">删除</a> </td> </tr> </c:forEach> </tbody> </table> <table border="0" cellspacing="0" cellpadding="0" width="900px"> <tr> <td class="td2"> <span>第${requestScope.pagemsg.currentPage }/ ${requestScope.pagemsg.totalPage}页</span> <span>总记录数:${requestScope.pagemsg.totalCount } 每页显示:${requestScope.pagemsg.pageSize}</span> <span> <p style="text-align: right"> <c:if test="${requestScope.pagemsg.currentPage != 1}"> <a href="${pageContext.request.contextPath }/employee/list?currentPage=1">[首页]</a> <a href="${pageContext.request.contextPath }/employee/list?currentPage=${requestScope.pagemsg.currentPage-1}">[上一页]</a> </c:if> <c:if test="${requestScope.pagemsg.currentPage != requestScope.pagemsg.totalPage}"> <a href="${pageContext.request.contextPath }/employee/list?currentPage=${requestScope.pagemsg.currentPage+1}">[下一页]</a> <a href="${pageContext.request.contextPath }/employee/list?currentPage=${requestScope.pagemsg.totalPage}">[尾页]</a> </c:if> </p> </span> </td> </tr> </table>1.7 效果图
2.1登录控制器代码
@RequestMapping("/login") public String login(HttpSession session, @RequestParam String sn, @RequestParam String password, HttpServletRequest request, HttpServletResponse response){ Employee employee = globalBiz.login(sn,password); if (employee == null) { return "redirect:to_login"; } else{ session.setAttribute("employee",employee); String sn1= request.getParameter("sn"); String pwd=request.getParameter("password"); String ck=request.getParameter("ck"); Cookie [] cookies=request.getCookies(); if ("on".equals(ck)){//判断前端页面是否勾选了记住密码 Cookie cookie=new Cookie("userinfo",sn1+"_"+pwd); cookie.setMaxAge(60); response.addCookie(cookie); } else{ Cookie cookie = new Cookie("userinfo",null); cookie.setMaxAge(0); //储存 cookie.setPath("/"); response.addCookie(cookie); } } return "redirect:self"; }2.2 jsp页面
<form method="post" action="login" id="contact"> <% String sn=null; String password=null; Cookie[] cookies=request.getCookies(); for (Cookie cookie:cookies){ if (cookie.getName().equals("userinfo")){ sn = cookie.getValue().split("_")[0]; password = cookie.getValue().split("_")[1]; //再一次的存起来(备用) request.setAttribute("sn", sn); request.setAttribute("password", password); } } %> <input type="text" name="sn" id="sn" class="gui-input" placeholder="请输入工号..." value="${sn}" /> <input type="password" name="password" id="password" class="gui-input" placeholder="请输入密码..." value="${password}" />