Spring_藉助Thymeleaf實現表單綁定

只要目標專注而不三心二意,鍥而不捨而不半途而廢,就必定可以實現咱們美好的理想。html

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

<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>指定綁定對象的屬性。
code

相關文章
相關標籤/搜索