目录
1.在UserDao接口中,添加Count方法
2.在UserDaoImp类中实现这个方法
3.在entity层中,添加PageUtil类
4.在service层中,UserService接口添加代码
5.在serviceImp层中,UserServiceImp类中添加代码
6.在Controller中,添加ListByPageServlet类
7.编写index.jsp
8.查看列表效果
9.在Controller中,添加LoadUpdateUserServlet类
10.添加updateStatus.jsp
11.运行效果
12.在Controller中,添加UpdateStatusServlet类
13.在Controller中,添加LoginServlet类,实现登陆功能
14.编写Login.jsp
15.项目结构
项目源码:链接:https://pan.baidu.com/s/1xwuJ6HNxl5wEf2JB9v84kQ 密码:5env
最终效果:
用来获取总数据量
/**
* 获取user的总数
* @return
*/
public int getCount();
@Override
public int getCount() {
Connection connection=JDBCUtil.getConnection();
PreparedStatement pstm=null;
ResultSet rs=null;
String sqlstring="select count(*) from user";
try {
pstm=connection.prepareStatement(sqlstring);
rs=pstm.executeQuery();
while(rs.next()){
return rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.closeAll(rs, pstm, connection);
}
return 0;
}
这里的PageUtil其实可以看做是视图模型也就是ViewModel,用来把数据传到视图上的模型
/**
*
* @author Administrator
* 分页的实体
*/
public class PageUtil {
/**
* 当前是第几页
*/
private int curIndex;
/**
* 一页的数量
*/
private int pageSize;
/**
* 总页数
*/
private int totalPage;
/**
* 总数据:
*/
private int totalNum;
private List list;
public int getCurIndex() {
return curIndex;
}
public void setCurIndex(int curIndex) {
this.curIndex = curIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getTotalNum() {
return totalNum;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
}
@Override
public String toString() {
return "PageUtil [curIndex=" + curIndex + ", pageSize=" + pageSize
+ ", totalPage=" + totalPage + ", totalNum=" + totalNum
+ ", list=" + list + "]";
}
}
/**
* 登录业务
*/
public boolean login(String username,String pwd);
/**
* 根据用户名和密码找一个u对象
*/
public User findUserByName(String username,String pwd);
/**
* 分页显示用户
*/
public PageUtil listByPage(int curIndex,int pageSize);
public User findById(int id);
@Override
public boolean login(String username, String pwd) {
User u= dao.findUserByNamePwd(username, pwd);
if(u!=null){
return true;
}
return false;
}
@Override
public User findUserByName(String username, String pwd) {
return dao.findUserByNamePwd(username, pwd);
}
@Override
public PageUtil listByPage(int curIndex, int pageSize) {
PageUtil page=new PageUtil();
page.setCurIndex(curIndex);
page.setPageSize(pageSize);
page.setTotalNum(dao.getCount());
// 20 ---> 12 1.4 2
//20/12=1
int pages=(int)Math.ceil((double)dao.getCount()/pageSize);
page.setTotalPage(pages);
List list=dao.listByPage(curIndex, pageSize);
page.setList(list);
return page;
}
@Override
public User findById(int id) {
return dao.findUserById(id);
}
浏览器访问这个Servlet,先获取数据库里面的数据和设置每页显示的数据条数,把数据丢到PageUtil模型中。然后再把请求转发到index.jsp。而不是直接访问index.jsp这个页面
package com.ly.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ly.entity.PageUtil;
import com.ly.entity.User;
import com.ly.service.UserService;
import com.ly.servicelmp.UserServicelmp;
public class ListByPageServlet extends HttpServlet {
private static final long serialVersionUID = -5544844869939793697L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int curIndex=1;
int pageSize=10;
String cur=req.getParameter("curIndex");
if (cur!=null) {
curIndex=Integer.parseInt(cur);
}
UserService userService=new UserServicelmp();
PageUtil pages=userService.listByPage(curIndex, pageSize);
req.setAttribute("pages", pages);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
列表页
用户展示
序号
用户名
状态
角色
操作
${status.index+1 }
${u.username}
${sta}
${role}
修改状态
上一页
下一页
在点击上面的修改状态时,会携带ID访问这个Servlet,通过ID找出这条信息,再转发请求到updateStatus.jsp页面
package com.ly.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ly.entity.User;
import com.ly.service.UserService;
import com.ly.servicelmp.UserServicelmp;
public class LoadUpdateUserServlet extends HttpServlet {
private static final long serialVersionUID = 185829281120159402L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String sid= req.getParameter("id");
UserService us=new UserServicelmp();
User u=us.findById(Integer.parseInt(sid));
if(u!=null){
req.setAttribute("u", u);
req.getRequestDispatcher("updateStatus.jsp").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
修改页面
修改人员的状态:离职
${msg}
点击上面的修改按钮会访问这个Servlet,获取提交上来的用户名和状态,再更新到数据库。
package com.ly.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ly.service.UserService;
import com.ly.servicelmp.UserServicelmp;
public class UpdateStatusServlet extends HttpServlet {
private static final long serialVersionUID = -7611113973578900805L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String status= req.getParameter("status");
String sid= req.getParameter("id");
if(status!=null && sid!=null){
UserService us=new UserServicelmp();
boolean flag= us.updateStatus(Integer.parseInt(sid), Integer.parseInt(status));
if(flag){
req.getRequestDispatcher("ListByPageServlet").forward(req, resp);
}else{
String msg="修改失败!";
req.setAttribute("msg", msg);
req.getRequestDispatcher("updateStatus.jsp").forward(req, resp);
}
}
}
}
package com.ly.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ly.entity.User;
import com.ly.service.UserService;
import com.ly.servicelmp.UserServicelmp;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 6392410094721738202L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
// 第一:获取请求的参数
String user = req.getParameter("user");
String pwd = req.getParameter("pwd");
System.out.println(user+"+"+pwd);
// 第二步:调用业务层:注册的业务即可
// 父类的应用指向子类的实例
UserService us = new UserServicelmp();
// 第二调用业务层逻辑
boolean flag = us.login(user, pwd);
if (flag) {
// 如果是超级管理员,直接讲所有的用户进行分页显示
User u = us.findUserByName(user, pwd);
HttpSession session = req.getSession();
session.setAttribute("user", u);
if (u != null && u.getRole() == 2) {
// 分页显示对象的用户
req.getRequestDispatcher("ListByPageServlet").forward(req, resp);
}
} else {
System.out.println("转到登陆界面");
resp.sendRedirect("login.jsp");
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here