项目场景:
通过查询用户的name来得到对应的数据并实现分页效果。
问题描述:
二次调用接口时name为null。
原因分析:
- 第一次调用查询接口时name属性是从登录接口获取
- 跳转到对应的页面后点击上一页或下一页按钮时没有拼接对应的name。
- jsp跳转查询接口时,name为null。
解决方案:
整体思路:
- 在Page里边添加Student成员变量。
- 通过第一次从登录接口获取到name,新建一个Student类
- 通过set方法将信息保存在Page中
- jsp页面获取Page里边的用户信息并拼接在有关分页的按钮中。
代码
Page类
package com.d.bean;
import java.util.List;
/**
* Created with IntelliJ IDEA
* Description:
* Author: sudi
* Date: 2020/9/1
* TIME: 21:59
*/
public class Page<T> {
private Integer currentPage = 1;
private Integer pageCount = 4;
private Integer totalCount;
private Integer totalPage;
private List<T> pageData;
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageCount() {
return pageCount;
}
public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalPage() {
if (totalCount % pageCount == 0) {
totalPage = totalCount / pageCount;
} else {
totalPage = totalCount / pageCount + 1;
}
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List<T> getPageData() {
return pageData;
}
public void setPageData(List<T> pageData) {
this.pageData = pageData;
}
}
jsp页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: sudi
Date: 2020/8/30
Time: 20:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1>studentIndex</h1>
<table class="table table-hover" id="studentTable">
<thead>
<tr>
<th></th>
<th>论文题目</th>
<th>发表刊物</th>
<th>作者</th>
<th>发表年份</th>
<th>是否通过审核</th>
<th>操作</th>
</tr>
</thead>
<c:forEach items="${thesis.pageData}" var="thesis">
<tbody>
<tr>
<td>${thesis.id}</td>
<td>${thesis.title}</td>
<td>${thesis.type}</td>
<td>${thesis.author}</td>
<td>${thesis.year}</td>
<td>${thesis.check}</td>
<td>
<button type="button" class="btn btn-info"><a href="/thesis/updateThesis?thesisID=${thesis.id}">修改</a></button>
</td>
<td>
<button type="button" class="btn btn-info"><a href="/thesis/deleteThesis?thesisID=${thesis.id}">删除</a></button>
</td>
</tr>
</tbody>
</c:forEach>
<tr>
<td colspan="3" align="center">
当前${thesis.currentPage }/${thesis.totalPage }页
<a href="/thesis/selectThesis?currentPage=1&name=${thesis.student.name}">首页</a>
<a href="/thesis/selectThesis?currentPage=${thesis.currentPage-1}&name=${thesis.student.name}">上一页 </a>
<a href="/thesis/selectThesis?currentPage=${thesis.currentPage+1}&name=${thesis.student.name}">下一页 </a>
<a href="/thesis/selectThesis?currentPage=${thesis.totalPage}&name=${thesis.student.name}">末页</a>
</td>
</tr>
</table>
</body>
</html>
控制层(Controller层)
package com.d.controller;
import com.d.bean.Page;
import com.d.bean.Student;
import com.d.bean.Thesis;
import com.d.service.StudentThesisService;
import com.d.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* Created with IntelliJ IDEA
* Description:
* Author: sudi
* Date: 2020/9/1
* TIME: 13:38
*/
@RequestMapping("/thesis")
@Controller
public class StudentThesisController {
@Autowired
StudentThesisService studentThesisService;
@RequestMapping("/selectThesis")
public String selectThesis(String name, Model model, Integer currentPage) {
Page<Thesis> pageThesis = new Page<>();
Student student = new Student();
student.setName(name);
pageThesis.setStudent(student);
List<Thesis> thesis = studentThesisService.selectThesis(name);
if (currentPage == null) {
pageThesis.setCurrentPage(1); // 第一次访问,设置当前页为1;
} else {
pageThesis.setCurrentPage(currentPage);
}
int totalCount = thesis.size();//总条数
pageThesis.setTotalCount(totalCount);
//如果当前页<=0,将当前页设为1,若果当前页大于总页数,将当前页设置为最后一页
if (pageThesis.getCurrentPage() <= 0) {
pageThesis.setCurrentPage(1);
} else if (pageThesis.getCurrentPage() > pageThesis.getTotalPage()) {
pageThesis.setCurrentPage(pageThesis.getTotalPage());
}
Integer index = (pageThesis.getCurrentPage() - 1) * pageThesis.getPageCount();
Integer count = pageThesis.getPageCount();
List<Thesis> pageThesis1 = studentThesisService.selectThesisByPage(name, index, count);
pageThesis.setPageData(pageThesis1);
model.addAttribute("thesis", pageThesis);
return "studentIndex";
}
@RequestMapping("/updateThesis")
public String updateThesis(String thesisID, Model model) {
Thesis thesis = studentThesisService.selectThesisByID(thesisID);
model.addAttribute("thesis", thesis);
return "stuUpdateThesis";
}
@RequestMapping("/deleteThesis")
public String deleteThesis(String thesisID, Model model) {
Thesis thesis = studentThesisService.selectThesisByID(thesisID);
return "stuDeleteThesis";
}
}