功能实现
老师角色查看所有学生
获取学生列表和分页
1.修改index.jsp
不能直接跳stuList.jsp,没数据
<%if("teacher".equals(role)){%>
<a href="TeaInitModifyServlet?username=<%=username%>">修改信息</a>
<a href="GetStuListServlet?curPage=1">查看所有学生</a>
<%}%>
2.新增GetStuListServlet
拿数据和分页
@WebServlet("/GetStuListServlet")
public class GetStuListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获取当前页数
int curPage = Integer.parseInt(request.getParameter("curPage"));
//设置当前页的数据条数
int count = 15;
//计算偏移量
int offset = (curPage-1)*count;
//计算总页数
int totalPage;
try {
int allCount = DBUtils.getAllCount("student");
if(allCount % count == 0){
totalPage = allCount/count;
}else{
totalPage = allCount/count + 1;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
//从数据库获取学生的集合
List<Student> students = null;
try {
students = DBUtils.commonQueryList(Student.class, "select * from student limit ?,?", offset, count);
} catch (SQLException e) {
throw new RuntimeException(e);
}
//处理学生集合
List<StudentDto> studentDtos = DtoUtils.studentDtoListHandler(students);
//将数据存入到请求对象中
request.setAttribute("curPage",curPage);
request.setAttribute("totalPage",totalPage);
request.setAttribute("studentDtos",studentDtos);
//request.setAttribute("students",students);直接传
//跳转
request.getRequestDispatcher("stuList.jsp").forward(request,response);
}
}
修改DBUtils–分页
新增getAllCount()
/**
* 获取当前表的总条数
*/
public static int getAllCount(String table) throws SQLException {
Connection connection = getConnection();
String sql = "select count(1) from " + table;
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
if(resultSet.next()){
int allCount = resultSet.getInt(1);
return allCount;
}
return 0;
}
修改Student–分页
做假数据,在实体类里面可以写main方法,插入操作
//做分页的假数据
public static void main(String[] args) {
for (int i = 1; i < 100; i++) {
String username = "xiaohei" + i;
String password = "123123";
String name = "小黑" + i;
String sex = "man";
int age = 23;
String[] hobbies = {"football","basketball"};
try {
DBUtils.commonUpdate("insert into student(username,password,name,sex,age,hobbies) values(?,?,?,?,?,?)",username,password,name,sex,age, StringUtils.handleArray(hobbies));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
新增StudentDto–处理学生信息
分包新增信息处理类,以方便工具类对学生的信息处理
package com.ckl.dto;
import com.ckl.pojo.Student;
public class StudentDto {
private Student student;
private String sex;
private String hobbies;
//自动生成方法略
}
新增DtoUtils --处理学生信息
工具类,处理学生信息提升页面展示效果
public class DtoUtils {
public static StudentDto studentDtoHandler(Student student){
//处理学生的性别数据
String sex = student.getSex();
if("man".equals(sex)){
sex = "男";
}else if("woman".equals(sex)){
sex = "女";
}
//处理学生的爱好数据
String hobbies = student.getHobbies();
hobbies = hobbies.replaceAll("football","足球");
hobbies = hobbies.replaceAll("basketball","篮球");
hobbies = hobbies.replaceAll("shop","购物");
//创建StudentDto
StudentDto studentDto = new StudentDto(student, sex, hobbies);
return studentDto;
}
public static List<StudentDto> studentDtoListHandler(List<Student> students){
List<StudentDto> studentDtos = new ArrayList<>();
for (Student student : students) {
StudentDto studentDto = studentDtoHandler(student);
studentDtos.add(studentDto);
}
return studentDtos;
}
}
3.stuList.jsp
直接获取数据库的学生数据
//未处理
List<Student> students = (List<Student>) request.getAttribute("students");
<%for(Student student:students){%>
<th><%=student.getUsername()%></th>
<th><%=student.getName()%></th>
<th><%=student.getSex()%></th>
<th><%=student.getAge()%></th>
<th><%=student.getHobbies()%></th>
添加学生信息处理后获取
//处理后
<%@ page import="java.util.List" %>
<%@ page import="com.qf.pojo.Student" %>
<%@ page import="com.qf.dto.StudentDto" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
List<StudentDto> studentDtos = (List<StudentDto>) request.getAttribute("studentDtos");
int curPage = (int) request.getAttribute("curPage");
int totalPage = (int) request.getAttribute("totalPage");
%>
<button οnclick="goIndex()">返回</button>
<h1>学生列表页面</h1>
<table border="1" width="500px">
<tr>
<th>账号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>爱好</th>
<th>操作</th>
</tr>
//获取学生信息展示
<%for(StudentDto studentDto:studentDtos){%>
<tr>
<th><%=studentDto.getStudent().getUsername()%></th>
<th><%=studentDto.getStudent().getName()%></th>
<th><%=studentDto.getSex()%></th>
<th><%=studentDto.getStudent().getAge()%></th>
<th><%=studentDto.getHobbies()%></th>
<th>
<a href="#">修改</a>
<a href="#">删除</a>
</th>
</tr>
<%}%>
</table>
//分页
<a href="GetStuListServlet?curPage=1">首页</a>
<%if(curPage > 1){%>
<a href="GetStuListServlet?curPage=<%=curPage-1%>">上一页</a>
<%}%>
<%if(curPage < totalPage){%>
<a href="GetStuListServlet?curPage=<%=curPage+1%>">下一页</a>
<%}%>
<a href="GetStuListServlet?curPage=<%=totalPage%>">尾页</a>
<script type="text/javascript">
function goIndex(){
window.location = "index.jsp";
}
</script>
</body>
</html>
老师角色修改和删除学生信息
修改
修改stuList.jsp
<%for(StudentDto studentDto:studentDtos){%>
<tr>
<th>
<a href="StuInitModifyServlet?username=<%=studentDto.getStudent().getUsername()%>">修改</a>
<a href="StuDeleteServlet?username=<%=studentDto.getStudent().getUsername()%>">删除</a>
</th>
</tr>
<%}%>
修改StuModifyServlet
获取角色
通过角色判断修改
学生需要更新Session、Cookie里的数据,跳转详情页面
老师跳转GetStuListServlet,需要更新数据【因为修改了需要刷新页面】
// request.setAttribute("name",name);
// response.addCookie(CookieUtils.createCookie("name",name,60*60*24*5));
//
// response.sendRedirect("index.jsp");
String role = (String) request.getSession().getAttribute("role");
if("student".equals(role)){
//更新Session里的数据
request.getSession().setAttribute("name",name);
//更新Cookie里的数据
response.addCookie(CookieUtils.createCookie("name",name,60*60*24*5));
//跳转详情页面
response.sendRedirect("index.jsp");
}else if("teacher".equals(role)){
//跳转
response.sendRedirect("GetStuListServlet?curPage=1");
}
删除
新增StuDeleteServlet
设置编码格式,获取请求中的账号,通过学生账号删除学生,最后跳转GetStuListServlet同时更新数据
注意:实际开发项目中是不会直接用delete,而是改状态,数据库有提到
@WebServlet("/StuDeleteServlet")
public class StuDeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");
try {
DBUtils.commonUpdate("delete from student where username=?",username);
} catch (SQLException e) {
throw new RuntimeException(e);
}
//跳转
response.sendRedirect("GetStuListServlet?curPage=1");
}
}