jsp-curd+分页倒导航案例

效果图

bash 复制代码
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生管理</title>
    <!-- 引入Bootstrap的CDN -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入jQuery的CDN -->
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <h1 class="mt-5">学生管理</h1>
    <!-- 搜索表单 -->
    <div class="mb-3">
        <form id="searchForm" class="form-inline">
            <input type="text" id="keyword" class="form-control mr-2" placeholder="请输入关键词">
            <button type="button" class="btn btn-primary" onclick="searchStudents()">搜索</button>
        </form>
    </div>
    <!-- 新增学生按钮 -->
    <div class="mb-3">
        <button type="button" class="btn btn-success" onclick="showAddStudentModal()">新增学生</button>
    </div>
    <!-- 学生列表 -->
    <div id="studentTable" class="table-responsive"></div>
    <!-- 分页控件 -->
    <nav>
        <ul class="pagination" id="pagination"></ul>
    </nav>
    <!-- 模态框用于新增和编辑学生 -->
    <div class="modal fade" id="studentModal" tabindex="-1" aria-labelledby="studentModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="studentModalLabel">学生信息</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="studentForm">
                        <input type="hidden" id="studentId">
                        <div class="form-group">
                            <label for="name">姓名</label>
                            <input type="text" class="form-control" id="name" required>
                        </div>
                        <div class="form-group">
                            <label for="gender">性别</label>
                            <select class="form-control" id="gender" required>
                                <option value="M">男</option>
                                <option value="F">女</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="className">班级</label>
                            <input type="text" class="form-control" id="className" required>
                        </div>
                        <div class="form-group">
                            <label for="studentNumber">学号</label>
                            <input type="text" class="form-control" id="studentNumber" required>
                        </div>
                        <div class="form-group">
                            <label for="phoneNumber">手机号</label>
                            <input type="text" class="form-control" id="phoneNumber" required>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="saveStudent()">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    let currentPage = 1;
    let pageSize = 5;

    // 初始化加载学生列表
    $(document).ready(function() {
        loadStudents();
    });

    function loadStudents() {
        $.ajax({
            url: '/student',
            type: 'GET',
            data: {
                operation: 'findByKeyword',
                keyword: $('#keyword').val(),
                page: currentPage,
                pageSize: pageSize
            },
            success: function(response) {
                if (response.code === 200) {
                    renderStudentTable(response.data);
                    renderPagination(response.other);
                } else {
                    alert(response.msg);
                }
            }
        });
    }

    function renderStudentTable(students) {
        let table = `<table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>姓名</th>
                                    <th>性别</th>
                                    <th>班级</th>
                                    <th>学号</th>
                                    <th>手机号</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>`;
        students.forEach(student => {
            table += `<tr data-id="${student.id}" data-name="${student.name}" data-gender="${student.gender}"
                           data-classname="${student.className}" data-studentnumber="${student.studentNumber}" data-phonenumber="${student.phoneNumber}">
                            <td>${student.name}</td>
                            <td>${student.gender}</td>
                            <td>${student.className}</td>
                            <td>${student.studentNumber}</td>
                            <td>${student.phoneNumber}</td>
                            <td>
                                <button class="btn btn-sm btn-primary" onclick="editStudent(this)">编辑</button>
                                <button class="btn btn-sm btn-danger" onclick="deleteStudent(${student.id})">删除</button>
                            </td>
                          </tr>`;
        });
        table += `</tbody></table>`;
        $('#studentTable').html(table);
    }

    function renderPagination(totalCount) {
        let totalPages = Math.ceil(totalCount / pageSize);
        let pagination = '';
        for (let i = 1; i <= totalPages; i++) {
            pagination += `<li class="page-item ${i === currentPage ? 'active' : ''}">
                                <button class="page-link" onclick="changePage(${i})">${i}</button>
                               </li>`;
        }
        $('#pagination').html(pagination);
    }

    function changePage(page) {
        currentPage = page;
        loadStudents();
    }

    function searchStudents() {
        currentPage = 1;
        loadStudents();
    }

    function showAddStudentModal() {
        $('#studentId').val('');
        $('#name').val('');
        $('#gender').val('M');
        $('#className').val('');
        $('#studentNumber').val('');
        $('#phoneNumber').val('');
        $('#studentModalLabel').text('新增学生');
        $('#studentModal').modal('show');
    }

    function editStudent(button) {
        let row = $(button).closest('tr');
        $('#studentId').val(row.data('id'));
        $('#name').val(row.data('name'));
        $('#gender').val(row.data('gender'));
        $('#className').val(row.data('classname'));
        $('#studentNumber').val(row.data('studentnumber'));
        $('#phoneNumber').val(row.data('phonenumber'));
        $('#studentModalLabel').text('编辑学生');
        $('#studentModal').modal('show');
    }

    function saveStudent() {
        let student = {
            id: $('#studentId').val(),
            name: $('#name').val(),
            gender: $('#gender').val(),
            className: $('#className').val(),
            studentNumber: $('#studentNumber').val(),
            phoneNumber: $('#phoneNumber').val()
        };

        let operation = student.id ? 'updateStudent' : 'register';

        $.ajax({
            url: '/student',
            type: 'POST',
            data: {
                operation: operation,
                ...student
            },
            success: function(response) {
                if (response.code === 200) {
                    $('#studentModal').modal('hide');
                    loadStudents();
                } else {
                    alert(response.msg);
                }
            }
        });
    }

    function deleteStudent(id) {
        if (confirm('确定要删除这个学生吗?')) {
            $.ajax({
                url: '/student',
                type: 'POST',
                data: {
                    operation: 'deleteStudent',
                    studentId: id
                },
                success: function(response) {
                    if (response.code === 200) {
                        loadStudents();
                    } else {
                        alert(response.msg);
                    }
                }
            });
        }
    }
</script>
</body>
</html>
相关推荐
残月只会敲键盘几秒前
面相小白的php反序列化漏洞原理剖析
开发语言·php
ac-er88883 分钟前
PHP弱类型安全问题
开发语言·安全·php
ac-er88884 分钟前
PHP网络爬虫常见的反爬策略
开发语言·爬虫·php
爱吃喵的鲤鱼14 分钟前
linux进程的状态之环境变量
linux·运维·服务器·开发语言·c++
LuckyLay19 分钟前
Spring学习笔记_27——@EnableLoadTimeWeaving
java·spring boot·spring
向阳121832 分钟前
Dubbo负载均衡
java·运维·负载均衡·dubbo
DARLING Zero two♡40 分钟前
关于我、重生到500年前凭借C语言改变世界科技vlog.16——万字详解指针概念及技巧
c语言·开发语言·科技
Gu Gu Study42 分钟前
【用Java学习数据结构系列】泛型上界与通配符上界
java·开发语言
芊寻(嵌入式)1 小时前
C转C++学习笔记--基础知识摘录总结
开发语言·c++·笔记·学习
WaaTong1 小时前
《重学Java设计模式》之 原型模式
java·设计模式·原型模式