Thymeleaf send parameter from html to controller

2020-03-20 10:25发布

问题:

I'm newbie in Thymeleaf. I'm trying to create simple crud application. I'm trying to delete object of Customer class on delete button. How can I set parameter(for example - id) to the method which called deleteUser using Thymeleaf. Here's my controller.

package controllers;

//imports


@Controller
public class WebController extends WebMvcConfigurerAdapter {

    @Autowired
    private CustomerDAO customerDAO;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/results").setViewName("results");
    }

    //show all users
    @RequestMapping(value="/users", method=RequestMethod.GET)
    public String contacts(Model model) {
        model.addAttribute("users",customerDAO.findAll());
        return "list";
    }

    //show form
    @RequestMapping(value="/users/add", method=RequestMethod.GET)
    public String showForm(Customer customer) {
        return "form";
    }

    //add user
    @RequestMapping(value="/users/doAdd", method=RequestMethod.POST)
    public String addUser(@RequestParam("firstName") String firstName,
                           @RequestParam("lastName") String lastName,
                           @RequestParam("lastName") String email) {
        customerDAO.save(new Customer(firstName, lastName, email));
        return "redirect:/users";
    }

    //delete user
    @RequestMapping(value="users/doDelete/{id}", method = RequestMethod.POST)
    public String deleteUser (@PathVariable Long id) {
        customerDAO.delete(id);
        return "redirect:/users";
    }
}

Here's my view.

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
List of users
<a href="users/add">Add new user</a>
<table>
    <tr>
        <th>Id</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Action</th>
    </tr>
    <tr th:each="user : ${users}">
        <td th:text="${user.id}">Id</td>
        <td th:text="${user.firstName}">First name</td>
        <td th:text="${user.lastName}">Last Name</td>
        <td th:text="${user.email}">Email</td>
        <td>
            <form th:action="@{/users/doDelete/}" th:object="${customer}" method="post">
                <button type="submit">Delete</button>
            </form>
        </td>
    </tr>
</table>
</body>
</html>

回答1:

You do not need form to do this:

<td>
    <a th:href="@{'/users/doDelete/' + ${user.id}}">
        <span>Delete</span>
    </a>
</td>



回答2:

Blejzer answer is an easy and straight forward solution unless you are also working with spring security (always recommended) in which case you should prefer POST instead of GET for all modification operations such as delete to protect against CSRF attack. This is exactly why spring recommends logout be done like this only. In order for you to adapt to POST, change your controller to read this parameter from request parameters instead of path variable

//delete user
@RequestMapping(value="users/doDelete", method = RequestMethod.POST)
public String deleteUser (@RequestParam Long id) {
    customerDAO.delete(id);
    return "redirect:/users";
}

Add a hidden field in the form which posts with the id as name and it's value in hidden parameter

<form th:action="@{/users/doDelete}" th:object="${customer}" method="post">
    <input type="hidden" th:field="${id}" />
    <button type="submit">Delete</button>
</form>

On a side note, even if you are not using spring security, it is always recommended to use post for any entity modification operations (like delete or update). Saves you from lots of trouble on web in long run. Take a look at Quick Checklist for Choosing HTTP GET or POST for detailed information.



回答3:

path variables can be set as:

<a th:href="@{/users/doDelete/__${user.id}__}"><span>Delete</span></a>