Thymeleaf 将参数从 html 发送到控制器 [英] Thymeleaf send parameter from html to controller

查看:27
本文介绍了Thymeleaf 将参数从 html 发送到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Thymeleaf 的新手.我正在尝试创建简单的 crud 应用程序.我正在尝试在删除按钮上删除 Customer 类的对象.如何将参数(例如 - id)设置为使用 Thymeleaf 调用 deleteUser 的方法.这是我的控制器.

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

这是我的看法.

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

推荐答案

您不需要表单来执行此操作:

You do not need form to do this:

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

这篇关于Thymeleaf 将参数从 html 发送到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆