用javaweb编写学生信息分页查询页面
时间: 2024-06-13 11:08:22 浏览: 128
java 编的 学生信息查询
以下是使用JavaWeb编写学生信息分页查询页面的步骤:
1.创建一个JSP页面,用于显示学生信息列表和分页导航栏。
2.创建一个Servlet,用于处理分页查询请求。在Servlet中,首先获取当前页码和每页显示的记录数,然后调用Service层的方法查询学生信息列表和总记录数。最后将查询结果存储到request中,转发到JSP页面进行显示。
3.在Service层中,使用DOM4J读取XML文件,解析出学生信息列表和总记录数。然后根据当前页码和每页显示的记录数,计算出需要显示的学生信息列表和总页数。
4.在DAO层中,使用XPath表达式查询XML文件中的学生信息。
5.在JSP页面中,使用JSTL标签库和EL表达式显示学生信息列表和分页导航栏。
以下是代码实现:
1. JSP页面
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息列表</title>
</head>
<body>
<h1>学生信息列表</h1>
<table border="1">
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.gender}</td>
<td>${student.age}</td>
</tr>
</c:forEach>
</table>
<c:if test="${totalPage > 1}">
<div>
<c:choose>
<c:when test="${currentPage > 1}">
<a href="student?page=${currentPage - 1}">上一页</a>
</c:when>
<c:otherwise>
上一页
</c:otherwise>
</c:choose>
<c:forEach begin="1" end="${totalPage}" var="i">
<c:choose>
<c:when test="${i == currentPage}">
<span>${i}</span>
</c:when>
<c:otherwise>
<a href="student?page=${i}">${i}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${currentPage < totalPage}">
<a href="student?page=${currentPage + 1}">下一页</a>
</c:when>
<c:otherwise>
下一页
</c:otherwise>
</c:choose>
</div>
</c:if>
</body>
</html>
```
2. Servlet
```java
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private StudentService studentService = new StudentServiceImpl();
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int page = Integer.parseInt(request.getParameter("page"));
int pageSize = 10;
List<Student> students = studentService.getStudentsByPage(page, pageSize);
int totalPage = studentService.getTotalPage(pageSize);
request.setAttribute("students", students);
request.setAttribute("currentPage", page);
request.setAttribute("totalPage", totalPage);
request.getRequestDispatcher("student.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
```
3. Service
```java
public class StudentServiceImpl implements StudentService {
private static final String FILE_NAME = "students.xml";
private static final String ROOT_ELEMENT_NAME = "students";
private static final String STUDENT_ELEMENT_NAME = "student";
private static final String ID_ELEMENT_NAME = "id";
private static final String NAME_ELEMENT_NAME = "name";
private static final String GENDER_ELEMENT_NAME = "gender";
private static final String AGE_ELEMENT_NAME = "age";
@Override
public List<Student> getStudentsByPage(int page, int pageSize) {
List<Student> students = new ArrayList<>();
try {
Document document = getDocument();
Element rootElement = document.getRootElement();
List<Element> studentElements = rootElement.elements(STUDENT_ELEMENT_NAME);
int startIndex = (page - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, studentElements.size());
for (int i = startIndex; i < endIndex; i++) {
Element studentElement = studentElements.get(i);
int id = Integer.parseInt(studentElement.elementText(ID_ELEMENT_NAME));
String name = studentElement.elementText(NAME_ELEMENT_NAME);
String gender = studentElement.elementText(GENDER_ELEMENT_NAME);
int age = Integer.parseInt(studentElement.elementText(AGE_ELEMENT_NAME));
Student student = new Student(id, name, gender, age);
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
return students;
}
@Override
public int getTotalPage(int pageSize) {
int totalPage = 0;
try {
Document document = getDocument();
Element rootElement = document.getRootElement();
List<Element> studentElements = rootElement.elements(STUDENT_ELEMENT_NAME);
int totalRecord = studentElements.size();
totalPage = (totalRecord + pageSize - 1) / pageSize;
} catch (Exception e) {
e.printStackTrace();
}
return totalPage;
}
private Document getDocument() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(FILE_NAME));
return document;
}
}
```
4. DAO
```java
public class StudentDaoImpl implements StudentDao {
private static final String FILE_NAME = "students.xml";
private static final String ROOT_ELEMENT_NAME = "students";
private static final String STUDENT_ELEMENT_NAME = "student";
private static final String ID_ELEMENT_NAME = "id";
private static final String NAME_ELEMENT_NAME = "name";
private static final String GENDER_ELEMENT_NAME = "gender";
private static final String AGE_ELEMENT_NAME = "age";
@Override
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
try {
Document document = getDocument();
Element rootElement = document.getRootElement();
List<Element> studentElements = rootElement.elements(STUDENT_ELEMENT_NAME);
for (Element studentElement : studentElements) {
int id = Integer.parseInt(studentElement.elementText(ID_ELEMENT_NAME));
String name = studentElement.elementText(NAME_ELEMENT_NAME);
String gender = studentElement.elementText(GENDER_ELEMENT_NAME);
int age = Integer.parseInt(studentElement.elementText(AGE_ELEMENT_NAME));
Student student = new Student(id, name, gender, age);
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
return students;
}
private Document getDocument() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(FILE_NAME));
return document;
}
}
```
阅读全文