Bootstrap和Java分页实例第二篇
在上一篇文章中,我们介绍了如何使用Bootstrap和Spring Boot实现基本的分页功能,本文将进一步探讨如何优化和扩展这些功能,以满足更复杂的需求。
环境准备
确保你已经安装了以下工具和库:
JDK 8或更高版本
Maven 3.2+
Spring Boot 2.3+
Thymeleaf
Bootstrap 4.3+
H2数据库(或其他任何关系型数据库)
项目结构
假设你的项目结构如下:
src/main/java/com/example/pagination |-PaginationApplication.java |-controller | |-PaginationController.java |-service | |-PaginationService.java | `-PaginationServiceImpl.java |-repository | `-UserRepository.java `-model `-User.java
1. 配置数据源
在application.properties
文件中配置H2数据库:
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.h2.console.enabled=true
2. 创建实体类
在model
包下创建User.java
:
package com.example.pagination.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private int age; // Getters and Setters }
3. 创建仓库接口
在repository
包下创建UserRepository.java
:
package com.example.pagination.repository; import com.example.pagination.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { }
4. 创建服务层
在service
包下创建PaginationService.java
和PaginationServiceImpl.java
:
package com.example.pagination.service; import com.example.pagination.model.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface PaginationService { Page<User> getUsers(Pageable pageable); }
package com.example.pagination.service; import com.example.pagination.model.User; import com.example.pagination.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @Service public class PaginationServiceImpl implements PaginationService { @Autowired private UserRepository userRepository; @Override public Page<User> getUsers(Pageable pageable) { return userRepository.findAll(pageable); } }
5. 创建控制器
在controller
包下创建PaginationController.java
:
package com.example.pagination.controller; import com.example.pagination.model.User; import com.example.pagination.service.PaginationService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class PaginationController { @Autowired private PaginationService paginationService; @GetMapping("/users") public String getUsers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "5") int size, Model model) { Pageable pageable = PageRequest.of(page, size); Page<User> users = paginationService.getUsers(pageable); model.addAttribute("users", users); model.addAttribute("currentPage", page); model.addAttribute("totalPages", users.getTotalPages()); return "users"; } }
6. 创建视图模板
在src/main/resources/templates
目录下创建users.html
:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Users</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1>Users List</h1> <table class="table table-bordered"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr th:each="user : ${users}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> </tr> </tbody> </table> <nav aria-label="Page navigation"> <ul class="pagination"> <li class="page-item" th:classappend="${#numbers.sequence(1, currentPage 1)} ? 'disabled' : ''"><a class="page-link" th:href="@{/users(page=${currentPage 1}, size=${size})}">«</a></li> <li class="page-item" th:each="i : ${#numbers.sequence(1, totalPages)}"><a class="page-link" th:href="@{/users(page=${i 1}, size=${size})}" th:text="${i}"></a></li> <li class="page-item" th:classappend="${#numbers.sequence(currentPage + 1, totalPages)} ? 'disabled' : ''"><a class="page-link" th:href="@{/users(page=${currentPage + 1}, size=${size})}">»</a></li> </ul> </nav> </div> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> </body> </html>
7. 运行应用程序
启动Spring Boot应用程序,访问http://localhost:8080/users
,你应该能看到分页的用户列表,你可以通过点击分页导航来切换不同的页面。
相关问题与解答栏目
问题1:如何自定义每页显示的记录数?
答:你可以在控制器中添加一个新的参数size
,并传递给服务层。
@GetMapping("/users") public String getUsers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "5") int size, Model model) { Pageable pageable = PageRequest.of(page, size); Page<User> users = paginationService.getUsers(pageable); model.addAttribute("users", users); model.addAttribute("currentPage", page); model.addAttribute("totalPages", users.getTotalPages()); return "users"; }
然后在视图模板中添加一个表单来选择每页显示的记录数:
<form method="get" action="/users"> <select name="size" onchange="this.form.submit()"> <option value="5" th:selected="${size == 5}">5 per page</option> <option value="10" th:selected="${size == 10}">10 per page</option> <option value="20" th:selected="${size == 20}">20 per page</option> </select> </form>
这样,用户就可以通过下拉菜单选择每页显示的记录数了。
以上内容就是解答有关“Bootstrap和Java分页实例第二篇”的详细内容了,我相信这篇文章可以为您解决一些疑惑,有任何问题欢迎留言反馈,谢谢阅读。
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/716500.html