1. 程式人生 > >Spring_借助Thymeleaf實現表單綁定

Spring_借助Thymeleaf實現表單綁定

html 理想 font AS register ror eth span div

只要目標專一而不三心二意,持之以恒而不半途而廢,就一定能夠實現我們美好的理想。

  使用Thymeleaf將一個表單綁定到th:object指定的對象上。

<html xmlns="http://www.w3.org/1999/xhmtl"
      xmlns:th="http://www.thymeleaf.org"
>
<head>
<title>register</title>
</head>
<body>
    <form method="POST" th:object="${user}">
        <
div class="errors" th:if="${#fields.hasErrors(‘*‘)}"> <ul> <li th:each="err : ${#fields.erros(‘*‘)}" th:text="${err}" >Input is incorrect</li> </ul> </div> <label th:class
="${#fields.hasErrors(‘firstName‘)}?‘error‘"> First Name</label> <input type="text" th:field="*{firstName}" th:class="${#fields.hasErrors(‘firstName‘)}?‘error‘" /> <label th:class="${#fields.hasErrors(‘lastName‘)}?‘error‘"> Last Name
</label> <input type="text" th:field="*{lastName}" th:class="${#fields.hasErrors(‘lastName‘)}?‘error‘" /> </form> </body> </html>

  <th:field>指定綁定對象的屬性。

Spring_借助Thymeleaf實現表單綁定