2020/9/2 Daily debug: Realize the paging logic, and the name is null when calling the interface for the second time.

Project scene:

Get the corresponding data by querying the user's name and realize the paging effect.

Problem Description:

The name is null when calling the interface a second time.

Cause Analysis:

  1. When the query interface is called for the first time, the name attribute is obtained from the login interface
  2. When the previous or next button is clicked after jumping to the corresponding page, the corresponding name is not spliced.
  3. When jsp jumps to query the interface, the name is null.

solution:

the whole idea:

  1. Add Student member variables in the Page.
  2. Create a new Student class by obtaining the name from the login interface for the first time
  3. Save information in Page through the set method
  4. The jsp page obtains the user information in the Page and splices it into the buttons related to paging.

Code

Page class

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 page

<%@ 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/[email protected]/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>

Control layer (Controller layer)

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";
    }
}

Guess you like

Origin blog.csdn.net/qq1350975694/article/details/108362624