thymeleaf实现
时间: 2025-05-30 11:10:12 浏览: 17
### Thymeleaf 实现 HTML 表格固定 `<th>` 标签并分页显示 `<td>` 内容
为了实现 HTML 表格中的 `<th>` 标签固定,并在超过 10 条记录时通过分页和滚动方式查看 `<td>` 的内容,可以按照以下方式进行设计:
#### 1. **HTML 结构**
使用 `position: sticky` 属性来固定表头 `<th>`。以下是基于 Thymeleaf 的模板结构示例:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Fixed Header Table</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
thead th {
position: sticky;
top: 0;
background-color: #f2f2f2;
z-index: 1;
}
tbody tr:nth-child(even) {
background-color: lightblue;
}
tbody tr:nth-child(odd) {
background-color: pink;
}
.scrollable-table {
max-height: 300px;
overflow-y: auto;
display: block;
}
</style>
</head>
<body>
<h1>User List with Fixed Header and Pagination</h1>
<div class="scrollable-table">
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${page.content}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.email}"></td>
</tr>
</tbody>
</table>
</div>
<!-- 分页导航 -->
<nav aria-label="Page navigation example">
<ul class="pagination">
<!-- 上一页按钮 -->
<li th:class="${!page.first} ? 'page-item' : 'disabled page-item'">
<a class="page-link" href="#" th:href="@{/users(page=${page.number - 1})}" aria-label="Previous">上一页</a>
</li>
<!-- 显示当前页码 -->
<li th:each="i : ${#numbers.sequence(1, page.totalPages)}"
th:class="${i == (page.number + 1)} ? 'active page-item' : 'page-item'"
style="display:inline;">
<a class="page-link"
th:href="@{/users(page=${i - 1})}"
th:text="${i}"></a>
</li>
<!-- 下一页按钮 -->
<li th:class="${!page.last} ? 'page-item' : 'disabled page-item'">
<a class="page-link" href="#" th:href="@{/users(page=${page.number + 1})}" aria-label="Next">下一页</a>
</li>
</ul>
</nav>
</body>
</html>
```
---
#### 2. **CSS 解析**
- 使用 `position: sticky;` 和 `top: 0;` 将表头 `<th>` 固定在顶部[^1]。
- 设置 `.scrollable-table` 类的最大高度为 `300px` 并启用垂直滚动条 (`overflow-y: auto`),以便当数据量超出范围时可以通过滚动查看。
---
#### 3. **Controller 处理逻辑**
在 Spring Boot 中定义 Controller 方法处理分页请求:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
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 UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public String getUsers(@RequestParam(defaultValue = "0") int page,
Model model) {
Page<User> userPage = userService.getUserList(PageRequest.of(page, 10));
model.addAttribute("page", userPage);
return "users";
}
}
```
此处的关键在于设置每页最多显示 10 条记录,并将分页对象传递给前端视图模型[^4]。
---
#### 4. **Service 层实现**
服务层负责从数据库获取分页后的用户列表:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public Page<User> getUserList(Pageable pageable) {
return userRepository.findAll(pageable);
}
// 其他 CRUD 方法...
}
```
此部分利用 Spring Data JPA 提供的分页功能简化开发流程[^5]。
---
#### 5. **Repository 定义**
定义 Repository 接口继承自 `PagingAndSortingRepository` 或 `JpaRepository`:
```java
@Repository
public interface UserRepository extends PagingAndSortingRepository<User, Long> {}
```
这一步允许直接调用内置的分页方法[^4]。
---
### 总结
以上方案实现了以下几个目标:
1. 使用 CSS 的 `sticky` 特性使表头始终可见。
2. 借助 Spring Boot 和 Thymeleaf 动态渲染分页组件。
3. 当数据量大于设定阈值(如 10 条)时,提供滚动区域方便浏览更多内容。
---
阅读全文
相关推荐


















