0

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.

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'>

If this is impossible to generate using thymeleaf maybe there is another way to insert values to selectedUsers.

Html page :

<!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> 

Part of controller:

@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"; } 

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.