2020/9/2毎日のデバッグ:ページングロジックを実現し、2回目のインターフェースの呼び出し時に名前がnullになる。

プロジェクトシーン:

ユーザー名を照会して対応するデータを取得し、ページング効果を実現します。

問題の説明:

インターフェースを2回呼び出したときの名前はnullです。

原因分析:

  1. クエリインターフェースが初めて呼び出されたとき、名前属性はログインインターフェースから取得されます。
  2. 対応するページにジャンプした後、[前へ]または[次へ]ボタンをクリックしても、対応する名前は結合されません。
  3. jspがジャンプしてインターフェースを照会するとき、名前はnullです。

解決:

全体のアイデア:

  1. ページに生徒のメンバー変数を追加します。
  2. ログインインターフェースから初めて名前を取得して、新しいStudentクラスを作成します。
  3. setメソッドを使用してページに情報を保存する
  4. jspページは、ページ内のユーザー情報を取得し、ページングに関連するボタンに接続します。

コード

ページクラス

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/[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>

コントロール層(コントローラー層)

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

おすすめ

転載: blog.csdn.net/qq1350975694/article/details/108362624