thymeleaf获取传id的链接
时间: 2023-10-27 09:11:38 浏览: 29
假设你的id为1,可以使用Thymeleaf的url表达式来获取传递id的链接:
```html
<a th:href="@{/your-url/{id}(id=1)}">Link</a>
```
其中,`@{/your-url/{id}(id=1)}`表示生成一个链接,`{id}`表示占位符,`(id=1)`表示传递的参数,即id的值为1。
相关问题
thymeleaf前端带参跳转页面
Thymeleaf是一种用于在Web和独立环境中创建HTML5的XML / XHTML / HTML5模板的现代服务器端Java模板引擎。Thymeleaf可以集成到Spring框架中,用于创建动态网页。下面是Thymeleaf前端带参跳转页面的介绍:
在Thymeleaf中,使用@{}表示一个URL表达式,其中可以包含参数。例如,如果我们想要跳转到/user页面,并带有一个参数id,可以这样写:
```
<a th:href="@{/user(id=${userId})}">跳转到用户页面</a>
```
在上述代码中,`@{/user}`表示当前应用的根路径下的/user页面,`${userId}`表示id参数的值,然后使用`th:href`属性来生成带参的跳转链接。
在控制器中,我们可以使用`@RequestParam`注解来获取参数。例如,我们可以这样写:
```
@GetMapping("/user")
public String user(@RequestParam("id") Long id, Model model) {
// 根据id查询用户信息
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "user";
}
```
在上述代码中,使用`@RequestParam("id")`注解获取id参数的值,并将查询到的用户信息添加到Model对象中,在返回值中指定Thymeleaf模板名称即可。
用thymeleaf进行分页展示数据
Thymeleaf是一个用于Web和独立环境的现代服务器端Java模板引擎,它可以与Spring框架无缝集成。在Thymeleaf中展示分页数据需要以下步骤:
1. 在Controller层获取分页数据,并将数据存入Model对象中。
2. 在HTML页面中添加thymeleaf的命名空间:xmlns:th="http://www.thymeleaf.org"。
3. 在HTML页面中添加分页组件,可以使用Bootstrap等前端框架提供的分页组件,或者自己编写分页组件。
4. 在HTML页面中使用th:each遍历分页数据,并在分页组件中展示数据。
下面是一个使用Thymeleaf展示分页数据的示例:
Controller层代码:
```
@GetMapping("/users")
public String listUsers(Model model, @RequestParam(defaultValue = "0") int page) {
Pageable pageable = PageRequest.of(page, 10, Sort.by("id").descending());
Page<User> userPage = userRepository.findAll(pageable);
model.addAttribute("users", userPage.getContent());
model.addAttribute("currentPage", page);
model.addAttribute("totalPages", userPage.getTotalPages());
return "userList";
}
```
HTML页面代码:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>User List</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>User List</h1>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</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.email}"></td>
</tr>
</tbody>
</table>
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item" th:classappend="${currentPage == 0} ? disabled : ''">
<a class="page-link" th:href="@{/users(page=0)}">First</a>
</li>
<li class="page-item" th:classappend="${currentPage == 0} ? disabled : ''">
<a class="page-link" th:href="@{/users(page=${currentPage - 1})}">Previous</a>
</li>
<li class="page-item" th:classappend="${currentPage == totalPages - 1} ? disabled : ''">
<a class="page-link" th:href="@{/users(page=${currentPage + 1})}">Next</a>
</li>
<li class="page-item" th:classappend="${currentPage == totalPages - 1} ? disabled : ''">
<a class="page-link" th:href="@{/users(page=${totalPages - 1})}">Last</a>
</li>
</ul>
</nav>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>
</body>
</html>
```
在该示例中,使用Bootstrap提供的分页组件展示分页数据,并使用th:classappend属性动态添加disabled类,禁用上一页和下一页按钮。使用th:href属性动态生成分页链接。