Spring_借助Thymeleaf實現表單綁定


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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM