diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/TeacherController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/TeacherController.java new file mode 100644 index 0000000000..844d746084 --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/TeacherController.java @@ -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"; + } +} diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Teacher.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Teacher.java new file mode 100644 index 0000000000..ad43590818 --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Teacher.java @@ -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 courses = new ArrayList(); + + 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 getCourses() { + return courses; + } + + public void setCourses(List courses) { + this.courses = courses; + } + + public String getAdditionalSkills() { + return additionalSkills; + } + + public void setAdditionalSkills(String additionalSkills) { + this.additionalSkills = additionalSkills; + } + +} diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/utils/TeacherUtils.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/utils/TeacherUtils.java new file mode 100644 index 0000000000..1bb279cc92 --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/utils/TeacherUtils.java @@ -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 teachers = new ArrayList(); + + public static List 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; + } + +} diff --git a/spring-thymeleaf/src/main/resources/messages_en.properties b/spring-thymeleaf/src/main/resources/messages_en.properties index d29fd24088..373c20f1d1 100644 --- a/spring-thymeleaf/src/main/resources/messages_en.properties +++ b/spring-thymeleaf/src/main/resources/messages_en.properties @@ -1,9 +1,12 @@ -msg.id=ID -msg.name=Name -msg.gender=Gender -msg.percent=Percentage -welcome.message=Welcome Student !!! -msg.AddStudent=Add Student -msg.ListStudents=List Students -msg.Home=Home - +msg.id=ID +msg.name=Name +msg.gender=Gender +msg.percent=Percentage +welcome.message=Welcome Student !!! +msg.AddStudent=Add Student +msg.ListStudents=List Students +msg.Home=Home +msg.ListTeachers=List Teachers +msg.courses=Courses +msg.skills=Skills +msg.active=Active \ No newline at end of file diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html index aa4047641c..3d4f8b530f 100644 --- a/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html @@ -18,6 +18,9 @@ + + + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/listTeachers.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listTeachers.html new file mode 100644 index 0000000000..e0085d0387 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listTeachers.html @@ -0,0 +1,50 @@ + + + +Teacher List + + + +

Teacher List

+ + + + + + + + + + + + + +
+ + + + + +
+ + + Female Male + + NO COURSES YET! +
+
+
+
+
+ + + + diff --git a/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerIntegrationTest.java b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerIntegrationTest.java index 462e9e8c21..4a2a9974f1 100644 --- a/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerIntegrationTest.java +++ b/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/ExpressionUtilityObjectsControllerIntegrationTest.java @@ -59,5 +59,10 @@ public class ExpressionUtilityObjectsControllerIntegrationTest { public void testDates() throws Exception { 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")); + } }