【问题标题】:Whats wrong with my Thymeleaf form我的 Thymeleaf 表格有什么问题
【发布时间】:2017-09-20 06:07:16
【问题描述】:

尝试在百里香叶中创建表单,但我不断收到以下错误消息:

执行处理器“org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor”时出错(customer-form:19)

这是我的 Thymeleaf 模板:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Save Customer</title>
    <link type="text/css" rel="stylesheet" href="style.css"/>
    <link type="text/css" rel="stylesheet" href="add-customer-style.css"/>
</head>

<body>
    <div id="wrapper">
        <div id="header">
            <h2>Customer Relationship Manager</h2>
        </div>
    </div>

    <div id="container">
        <h3>Save Customer</h3>
        <form action="saveCustomer" th:object="${customer}" method="post"/>
        <table>
            <tbody>
                <tr>
                    <td><label>First Name</label></td>
                    <td><input type="text" th:field="*{firstName}"/></td>
                </tr>
            </tbody>
        </table>

    </div>
</body>
</html>

以及持有它的控制器:

@Controller
public class CustomerController {

    private ServiceDAO serviceDAO;

    @Autowired
    public void setServiceDAO(ServiceDAO serviceDAO) {
        this.serviceDAO = serviceDAO;
    }

    @RequestMapping("/")
    public String listCustomers(Model model){

        List<Customer> customers = serviceDAO.findAll();
        model.addAttribute("customers", customers);
        return "home";
    }

    @GetMapping("/ShowFormForAdd")
    public String ShowFormForAdd(Model model){

        Customer customer = new Customer();

        model.addAttribute("customer", customer);

        return "customer-form";
    }

}

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    确保在输入字段后关闭表单标签。其次,更新您的操作以让 Thymeleaf 正确解决它:

      <form th:action="@{/saveCustomer}" th:object="${customer}" method="post">
        <table>
            <tbody>
                <tr>
                    <td><label>First Name</label></td>
                    <td><input type="text" th:field="*{firstName}"/></td>
                </tr>
            </tbody>
        </table>
      </form>
    

    【讨论】:

    • 如果可行,只需用复选标记和/或投票进行标记。
    猜你喜欢
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多