Skip to content

Commit 3d82778

Browse files
igorpelemaibin
authored andcommitted
Thymeleaf conditionals (eugenp#3949)
* different types of thymeleaf conditionals * improved thymeleaf conditionals
1 parent 13bedab commit 3d82778

7 files changed

Lines changed: 214 additions & 9 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.thymeleaf.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
8+
import com.baeldung.thymeleaf.utils.TeacherUtils;
9+
10+
@Controller
11+
public class TeacherController {
12+
13+
@RequestMapping(value = "/listTeachers", method = RequestMethod.GET)
14+
public String getInfo(Model model) {
15+
model.addAttribute("teachers", TeacherUtils.buildTeachers());
16+
17+
return "listTeachers.html";
18+
}
19+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.thymeleaf.model;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import javax.validation.constraints.Min;
8+
import javax.validation.constraints.NotNull;
9+
10+
public class Teacher implements Serializable {
11+
12+
private static final long serialVersionUID = 946941572942270450L;
13+
14+
@NotNull(message = "Teacher ID is required.")
15+
@Min(value = 1000, message = "Teacher ID must be at least 4 digits.")
16+
private Integer id;
17+
18+
@NotNull(message = "Teacher name is required.")
19+
private String name;
20+
21+
@NotNull(message = "Teacher gender is required.")
22+
private String gender;
23+
24+
private boolean isActive;
25+
26+
private List<String> courses = new ArrayList<String>();
27+
28+
private String additionalSkills;
29+
30+
public Integer getId() {
31+
return id;
32+
}
33+
34+
public void setId(Integer id) {
35+
this.id = id;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
public String getGender() {
47+
return gender;
48+
}
49+
50+
public void setGender(String gender) {
51+
this.gender = gender;
52+
}
53+
54+
public boolean isActive() {
55+
return isActive;
56+
}
57+
58+
public void setActive(boolean isActive) {
59+
this.isActive = isActive;
60+
}
61+
62+
public List<String> getCourses() {
63+
return courses;
64+
}
65+
66+
public void setCourses(List<String> courses) {
67+
this.courses = courses;
68+
}
69+
70+
public String getAdditionalSkills() {
71+
return additionalSkills;
72+
}
73+
74+
public void setAdditionalSkills(String additionalSkills) {
75+
this.additionalSkills = additionalSkills;
76+
}
77+
78+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.thymeleaf.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.baeldung.thymeleaf.model.Teacher;
7+
8+
public class TeacherUtils {
9+
10+
private static List<Teacher> teachers = new ArrayList<Teacher>();
11+
12+
public static List<Teacher> buildTeachers() {
13+
if (teachers.isEmpty()) {
14+
Teacher teacher1 = new Teacher();
15+
teacher1.setId(2001);
16+
teacher1.setName("Jane Doe");
17+
teacher1.setGender("F");
18+
teacher1.setActive(true);
19+
teacher1.getCourses().add("Mathematics");
20+
teacher1.getCourses().add("Physics");
21+
22+
teachers.add(teacher1);
23+
24+
Teacher teacher2 = new Teacher();
25+
teacher2.setId(2002);
26+
teacher2.setName("Lazy Dude");
27+
teacher2.setGender("M");
28+
teacher2.setActive(false);
29+
teacher2.setAdditionalSkills("emergency responder");
30+
31+
teachers.add(teacher2);
32+
33+
Teacher teacher3 = new Teacher();
34+
teacher3.setId(2002);
35+
teacher3.setName("Micheal Jordan");
36+
teacher3.setGender("M");
37+
teacher3.setActive(true);
38+
teacher3.getCourses().add("Sports");
39+
40+
teachers.add(teacher3);
41+
42+
}
43+
44+
return teachers;
45+
}
46+
47+
}
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
msg.id=ID
2-
msg.name=Name
3-
msg.gender=Gender
4-
msg.percent=Percentage
5-
welcome.message=Welcome Student !!!
6-
msg.AddStudent=Add Student
7-
msg.ListStudents=List Students
8-
msg.Home=Home
9-
1+
msg.id=ID
2+
msg.name=Name
3+
msg.gender=Gender
4+
msg.percent=Percentage
5+
welcome.message=Welcome Student !!!
6+
msg.AddStudent=Add Student
7+
msg.ListStudents=List Students
8+
msg.Home=Home
9+
msg.ListTeachers=List Teachers
10+
msg.courses=Courses
11+
msg.skills=Skills
12+
msg.active=Active

‎spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ <h1>
1818
<tr>
1919
<td><a th:href="@{/listStudents}" th:text="#{msg.ListStudents}" /></td>
2020
</tr>
21+
<tr>
22+
<td><a th:href="@{/listTeachers}" th:text="#{msg.ListTeachers}" /></td>
23+
</tr>
2124
</table>
2225
</body>
2326
</html>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<head>
5+
<title>Teacher List</title>
6+
</head>
7+
8+
<body>
9+
<h1>Teacher List</h1>
10+
<table border="1">
11+
<thead>
12+
<tr>
13+
<th th:text="#{msg.id}" />
14+
<th th:text="#{msg.name}" />
15+
<th th:text="#{msg.gender}" />
16+
<th th:text="#{msg.active}" />
17+
<th th:text="#{msg.courses}" />
18+
<th th:text="#{msg.skills}" />
19+
</tr>
20+
</thead>
21+
<tbody>
22+
<tr th:each="teacher: ${teachers}">
23+
<td th:text="${teacher.id}" />
24+
<td th:text="${{teacher.name}}" />
25+
26+
<td><span th:if="${teacher.gender == 'F'}">Female</span> <span
27+
th:unless="${teacher.gender == 'F'}">Male</span></td>
28+
29+
<td th:text="${teacher.active} ? 'ACTIVE' : 'RETIRED'" />
30+
31+
<td th:switch="${#lists.size(teacher.courses)}"><span
32+
th:case="'0'">NO COURSES YET!</span> <span th:case="'1'"
33+
th:text="${teacher.courses[0]}"></span>
34+
<div th:case="*">
35+
<div th:each="course: ${teacher.courses}" th:text="${course}"></div>
36+
</div></td>
37+
38+
<td th:text="*{teacher.additionalSkills}?: 'UNKNOWN'" />
39+
</tr>
40+
</tbody>
41+
</table>
42+
<div>
43+
<p>
44+
<a th:href="@{/}" th:text="#{msg.Home}" />
45+
</p>
46+
</div>
47+
</body>
48+
</html>
49+
50+

‎spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerIntegrationTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ public void testGetObjects() throws Exception {
5959
public void testDates() throws Exception {
6060
mockMvc.perform(get("/dates").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("dates.html"));
6161
}
62+
63+
@Test
64+
public void testTeachers() throws Exception {
65+
mockMvc.perform(get("/listTeachers").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("listTeachers.html"));
66+
}
6267

6368
}

0 commit comments

Comments
 (0)