43- (Very Important)Thymeleaf Attributes and their roles or working

Image 1 For Better understanding of th:each and model.addAttribute()


Lets understand with examples


1-    model.addAttribute()

   
model.addAttribute("students", studentService.getAllStudents());

This means  the value of      studentService.getAllStudents()  is given to  "students"



2-   th:each


<tr th:each="student:${students}">

This expression is used for iterating or creating a loop for students


3-   th:text

 
<td th:text="${student.firstName}" ></td>

Generating an input text for String firstName.


4-   th:href


<a th:href="@{/students/new}" class="btn btn-primary"> Add Student </a>

Context path for link basically anchor tag.


5- th:action ] & [ th:object ] & [ method


<form th:action="@{/students}" th:object = "${student}" method="POST">

th:action

In simple terms where the data will be posted to /students .

OR

Used to provide the form action URL.

OR

Is an expression which in this case will direct the form to POST to the /students endpoint.


th:object

Object(in this case student) that needs to be stored in the database.


method="POST"

To perform POST operation to the form.


6- th:field

  <input
                            type = "text"
                            name = "firstName"
                            th:field = "*{firstName}"
                            class = "form-control"
                            placeholder="Enter Student First Name"
                            />

Matching the String  firstName to the value of form.

Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot