使用Thymeleaf,Spring Boot生成超链接 [英] Generating hyperlinks with Thymeleaf, Spring Boot
问题描述
我在使用Thymeleaf生成html链接时遇到问题.我用过jpa,我有2个表("users"和"selectedUsers"),我制作了一个html页面,其中表"users"显示为列表.
I have a problem with generating html links using Thymeleaf. I've used jpa and I have 2 tables ('users' and 'selectedUsers') I' ve made a html page where table 'users' is displayed as a list.
它可以工作,但是我想在此列表的每个生成的元素中添加一个超链接,该超链接将为"selectedUsers"表添加值.控制器应该是正确的,但我不知道如何生成链接
It works, but I want to add to every generated element of this list a hyperlink which will add value to 'selectedUsers' table. Controller should be right, but I don't know how to generate a link
http://localhost:8080/selectedusers/create?name=<name of current element in 'users table'>
如果用百里香无法产生这种效果,也许还有另一种方法可以将值插入selectedUsers.
If this is impossible to generate using thymeleaf maybe there is another way to insert values to selectedUsers.
HTML页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>All users</title>
</head>
<body>
<div id="container">
<h1> List of all users: </h1>
<ul th:each="user : ${users}">
<div id="list">
User data:
<p th:text="${user.name} + ' | ' + ${user.email}"></p>
<a href="/selectedusers/create?name= ">Click here to add
this user to another table in database</a>
</div>
</ul>
</div>
</body>
</html>
控制器的一部分:
@GetMapping("/find-all-users")
public String findUsers(Model model) {
model.addAttribute("users", userDao.findAll());
return "findusers";
}
@Autowired
private SelectedUserDao selectedUserDao;
@RequestMapping("/selectedusers/create")
public String create(@RequestParam("name") String name) {
String selectedUserId="";
SelectedUser selectedUser= new SelectedUser(name);
selectedUserDao.save(selectedUser);
selectedUserId = String.valueOf(selectedUser.getId());
return "findusers";
}
推荐答案
类似于文档,您可以使用百里香标准方言.
Like described in the documentation you can use the thymeleaf standard dialects.
<a th:href="@{/selectedusers/create(name=${user.name})}">Click here to add
this user to another table in database</a>
内部会产生这样的链接:
Internally this will produce a link like that:
/selectedusers/create?name=anyName
这篇关于使用Thymeleaf,Spring Boot生成超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!