Thymeleaf conditionals (#3949)
* different types of thymeleaf conditionals * improved thymeleaf conditionals
This commit is contained in:
parent
13bedabb99
commit
3d827783f7
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.thymeleaf.controller;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
|
import com.baeldung.thymeleaf.utils.TeacherUtils;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class TeacherController {
|
||||||
|
|
||||||
|
@RequestMapping(value = "/listTeachers", method = RequestMethod.GET)
|
||||||
|
public String getInfo(Model model) {
|
||||||
|
model.addAttribute("teachers", TeacherUtils.buildTeachers());
|
||||||
|
|
||||||
|
return "listTeachers.html";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
package com.baeldung.thymeleaf.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class Teacher implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 946941572942270450L;
|
||||||
|
|
||||||
|
@NotNull(message = "Teacher ID is required.")
|
||||||
|
@Min(value = 1000, message = "Teacher ID must be at least 4 digits.")
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
@NotNull(message = "Teacher name is required.")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@NotNull(message = "Teacher gender is required.")
|
||||||
|
private String gender;
|
||||||
|
|
||||||
|
private boolean isActive;
|
||||||
|
|
||||||
|
private List<String> courses = new ArrayList<String>();
|
||||||
|
|
||||||
|
private String additionalSkills;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGender() {
|
||||||
|
return gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGender(String gender) {
|
||||||
|
this.gender = gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isActive() {
|
||||||
|
return isActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActive(boolean isActive) {
|
||||||
|
this.isActive = isActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getCourses() {
|
||||||
|
return courses;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCourses(List<String> courses) {
|
||||||
|
this.courses = courses;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAdditionalSkills() {
|
||||||
|
return additionalSkills;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdditionalSkills(String additionalSkills) {
|
||||||
|
this.additionalSkills = additionalSkills;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.baeldung.thymeleaf.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.baeldung.thymeleaf.model.Teacher;
|
||||||
|
|
||||||
|
public class TeacherUtils {
|
||||||
|
|
||||||
|
private static List<Teacher> teachers = new ArrayList<Teacher>();
|
||||||
|
|
||||||
|
public static List<Teacher> buildTeachers() {
|
||||||
|
if (teachers.isEmpty()) {
|
||||||
|
Teacher teacher1 = new Teacher();
|
||||||
|
teacher1.setId(2001);
|
||||||
|
teacher1.setName("Jane Doe");
|
||||||
|
teacher1.setGender("F");
|
||||||
|
teacher1.setActive(true);
|
||||||
|
teacher1.getCourses().add("Mathematics");
|
||||||
|
teacher1.getCourses().add("Physics");
|
||||||
|
|
||||||
|
teachers.add(teacher1);
|
||||||
|
|
||||||
|
Teacher teacher2 = new Teacher();
|
||||||
|
teacher2.setId(2002);
|
||||||
|
teacher2.setName("Lazy Dude");
|
||||||
|
teacher2.setGender("M");
|
||||||
|
teacher2.setActive(false);
|
||||||
|
teacher2.setAdditionalSkills("emergency responder");
|
||||||
|
|
||||||
|
teachers.add(teacher2);
|
||||||
|
|
||||||
|
Teacher teacher3 = new Teacher();
|
||||||
|
teacher3.setId(2002);
|
||||||
|
teacher3.setName("Micheal Jordan");
|
||||||
|
teacher3.setGender("M");
|
||||||
|
teacher3.setActive(true);
|
||||||
|
teacher3.getCourses().add("Sports");
|
||||||
|
|
||||||
|
teachers.add(teacher3);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return teachers;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,12 @@
|
||||||
msg.id=ID
|
msg.id=ID
|
||||||
msg.name=Name
|
msg.name=Name
|
||||||
msg.gender=Gender
|
msg.gender=Gender
|
||||||
msg.percent=Percentage
|
msg.percent=Percentage
|
||||||
welcome.message=Welcome Student !!!
|
welcome.message=Welcome Student !!!
|
||||||
msg.AddStudent=Add Student
|
msg.AddStudent=Add Student
|
||||||
msg.ListStudents=List Students
|
msg.ListStudents=List Students
|
||||||
msg.Home=Home
|
msg.Home=Home
|
||||||
|
msg.ListTeachers=List Teachers
|
||||||
|
msg.courses=Courses
|
||||||
|
msg.skills=Skills
|
||||||
|
msg.active=Active
|
|
@ -18,6 +18,9 @@
|
||||||
<tr>
|
<tr>
|
||||||
<td><a th:href="@{/listStudents}" th:text="#{msg.ListStudents}" /></td>
|
<td><a th:href="@{/listStudents}" th:text="#{msg.ListStudents}" /></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><a th:href="@{/listTeachers}" th:text="#{msg.ListTeachers}" /></td>
|
||||||
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<title>Teacher List</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Teacher List</h1>
|
||||||
|
<table border="1">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th th:text="#{msg.id}" />
|
||||||
|
<th th:text="#{msg.name}" />
|
||||||
|
<th th:text="#{msg.gender}" />
|
||||||
|
<th th:text="#{msg.active}" />
|
||||||
|
<th th:text="#{msg.courses}" />
|
||||||
|
<th th:text="#{msg.skills}" />
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr th:each="teacher: ${teachers}">
|
||||||
|
<td th:text="${teacher.id}" />
|
||||||
|
<td th:text="${{teacher.name}}" />
|
||||||
|
|
||||||
|
<td><span th:if="${teacher.gender == 'F'}">Female</span> <span
|
||||||
|
th:unless="${teacher.gender == 'F'}">Male</span></td>
|
||||||
|
|
||||||
|
<td th:text="${teacher.active} ? 'ACTIVE' : 'RETIRED'" />
|
||||||
|
|
||||||
|
<td th:switch="${#lists.size(teacher.courses)}"><span
|
||||||
|
th:case="'0'">NO COURSES YET!</span> <span th:case="'1'"
|
||||||
|
th:text="${teacher.courses[0]}"></span>
|
||||||
|
<div th:case="*">
|
||||||
|
<div th:each="course: ${teacher.courses}" th:text="${course}"></div>
|
||||||
|
</div></td>
|
||||||
|
|
||||||
|
<td th:text="*{teacher.additionalSkills}?: 'UNKNOWN'" />
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<div>
|
||||||
|
<p>
|
||||||
|
<a th:href="@{/}" th:text="#{msg.Home}" />
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
|
|
@ -59,5 +59,10 @@ public class ExpressionUtilityObjectsControllerIntegrationTest {
|
||||||
public void testDates() throws Exception {
|
public void testDates() throws Exception {
|
||||||
mockMvc.perform(get("/dates").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("dates.html"));
|
mockMvc.perform(get("/dates").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("dates.html"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTeachers() throws Exception {
|
||||||
|
mockMvc.perform(get("/listTeachers").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("listTeachers.html"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue