我的Thymeleaf形式出了什么问题

时间:2017-04-23 15:43:22

标签: spring-boot thymeleaf

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

执行处理器' org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'时出错。 (客户形式: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";
    }

}

1 个答案:

答案 0 :(得分:2)

确保在输入字段后关闭表单标记。其次,更新您的行动以让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>