JAVA-8281: split or move spring-thymeleaf module

This commit is contained in:
chaos2418
2021-12-18 17:40:12 +05:30
parent 273fd0792c
commit ff67d378b0
28 changed files with 518 additions and 44 deletions
@@ -1,46 +0,0 @@
package com.baeldung.thymeleaf.controller;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
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 org.springframework.web.bind.annotation.RequestParam;
import com.baeldung.thymeleaf.model.Book;
import com.baeldung.thymeleaf.service.BookService;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping(value = "/listBooks", method = RequestMethod.GET)
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
final int currentPage = page.orElse(1);
final int pageSize = size.orElse(5);
Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));
model.addAttribute("bookPage", bookPage);
int totalPages = bookPage.getTotalPages();
if (totalPages > 0) {
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
.boxed()
.collect(Collectors.toList());
model.addAttribute("pageNumbers", pageNumbers);
}
return "listBooks.html";
}
}
@@ -1,26 +0,0 @@
package com.baeldung.thymeleaf.controller;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*
*/
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getHome(Model model) {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.getDefault());
model.addAttribute("serverTime", dateFormat.format(new Date()));
return "home.html";
}
}
@@ -1,50 +0,0 @@
package com.baeldung.thymeleaf.controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baeldung.thymeleaf.model.Student;
import com.baeldung.thymeleaf.utils.StudentUtils;
/**
* Handles requests for the student model.
*
*/
@Controller
public class StudentController {
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
if (!errors.hasErrors()) {
// get mock objects
List<Student> students = StudentUtils.buildStudents();
// add current student
students.add(student);
model.addAttribute("students", students);
}
return ((errors.hasErrors()) ? "addStudent.html" : "listStudents.html");
}
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
public String addStudent(Model model) {
model.addAttribute("student", new Student());
return "addStudent.html";
}
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
public String listStudent(Model model) {
model.addAttribute("students", StudentUtils.buildStudents());
return "listStudents.html";
}
}
@@ -1,19 +0,0 @@
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";
}
}
@@ -1,29 +0,0 @@
package com.baeldung.thymeleaf.model;
public class Book {
private int id;
private String name;
public Book(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -1,78 +0,0 @@
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;
}
}
@@ -1,38 +0,0 @@
package com.baeldung.thymeleaf.service;
import java.util.Collections;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.baeldung.thymeleaf.model.Book;
import com.baeldung.thymeleaf.utils.BookUtils;
@Service
public class BookService {
final private List<Book> books = BookUtils.buildBooks();
public Page<Book> findPaginated(Pageable pageable) {
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
List<Book> list;
if (books.size() < startItem) {
list = Collections.emptyList();
} else {
int toIndex = Math.min(startItem + pageSize, books.size());
list = books.subList(startItem, toIndex);
}
Page<Book> bookPage = new PageImpl<Book>(list, PageRequest.of(currentPage, pageSize), books.size());
return bookPage;
}
}
@@ -1,28 +0,0 @@
package com.baeldung.thymeleaf.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import com.baeldung.thymeleaf.model.Book;
public class BookUtils {
private static List<Book> books = new ArrayList<Book>();
private static final int NUM_BOOKS = 30;
private static final int MIN_BOOK_NUM = 1000;
public static List<Book> buildBooks() {
if (books.isEmpty()) {
IntStream.range(0, NUM_BOOKS).forEach(n -> {
books.add(new Book(MIN_BOOK_NUM + n + 1, "Spring in Action"));
});
}
return books;
}
}
@@ -1,47 +0,0 @@
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,45 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Add Student</title>
</head>
<body>
<h1>Add Student</h1>
<form action="#" th:action="@{/saveStudent}" th:object="${student}"
method="post">
<ul>
<li th:errors="*{id}" />
<li th:errors="*{name}" />
<li th:errors="*{gender}" />
<li th:errors="*{percentage}" />
</ul>
<table border="1">
<tr>
<td><label th:text="#{msg.id}" /></td>
<td><input type="number" th:field="*{id}" /></td>
</tr>
<tr>
<td><label th:text="#{msg.name}" /></td>
<td><input type="text" th:field="*{name}" /></td>
</tr>
<tr>
<td><label th:text="#{msg.gender}" /></td>
<td><select th:field="*{gender}">
<option th:value="'M'" th:text="Male"></option>
<option th:value="'F'" th:text="Female"></option>
</select></td>
</tr>
<tr>
<td><label th:text="#{msg.percent}" /></td>
<td><select id="percentage" name="percentage">
<option th:each="i : ${#numbers.sequence(0, 100)}" th:value="${i}" th:text="${i}" th:selected="${i==75}"></option>
</select></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
@@ -1,29 +0,0 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
th:with="lang=${#locale.language}" th:lang="${lang}">
<head>
<title>Home</title>
</head>
<body>
<h1>
<span th:text="#{welcome.message}" />
</h1>
<p>
Current time is <span th:text="${serverTime}" />
</p>
<table>
<tr>
<td><a th:href="@{/addStudent}" th:text="#{msg.AddStudent}" /></td>
</tr>
<tr>
<td><a th:href="@{/listStudents}" th:text="#{msg.ListStudents}" /></td>
</tr>
<tr>
<td><a th:href="@{/listTeachers}" th:text="#{msg.ListTeachers}" /></td>
</tr>
<tr>
<td><a th:href="@{/listBooks}" th:text="#{msg.ListBooks}" /></td>
</tr>
</table>
</body>
</html>
@@ -1,58 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<style>
.pagination {
display: inline-block;
}
.pagination a {
color: black;
float: left;
padding: 5px 5px;
text-decoration: none;
}
.pagination a.active {
background-color: gray;
color: white;
border-radius: 2px;
}
</style>
<head>
<title>Book List</title>
</head>
<body>
<h1>Book List</h1>
<table border="1">
<thead>
<tr>
<th th:text="#{msg.id}" />
<th th:text="#{msg.name}" />
</tr>
</thead>
<tbody>
<tr th:each="book, iStat : ${bookPage.content}"
th:style="${iStat.odd}? 'font-weight: bold;'"
th:alt-title="${iStat.even}? 'even' : 'odd'">
<td th:text="${book.id}" />
<td th:text="${book.name}" />
</tr>
</tbody>
</table>
<div th:if="${bookPage.totalPages > 0}" class="pagination"
th:each="pageNumber : ${pageNumbers}">
<a th:href="@{/listBooks(size=${bookPage.size}, page=${pageNumber})}"
th:text=${pageNumber}
th:class="${pageNumber==bookPage.number + 1} ? active"></a>
</div>
<div>
<p>
<a th:href="@{/}" th:text="#{msg.Home}"></a>
</p>
</div>
</body>
</html>
@@ -1,53 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Student List</title>
</head>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url : "/spring-thymeleaf/js",
});
});
</script>
<body>
<h1>Student 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.percent}" />
<th th:text="index" />
<th th:text="count" />
<th th:text="first" />
<th th:text="last" />
</tr>
</thead>
<tbody>
<tr th:each="student, iStat : ${students}" th:style="${iStat.odd}? 'font-weight: bold;'" th:alt-title="${iStat.even}? 'even' : 'odd'">
<td th:text="${student.id}" />
<td th:text="${student.name}" />
<td th:switch="${student.gender}"><span th:case="'M'"
th:text="Male" /> <span th:case="'F'" th:text="Female" /></td>
<td th:text="${#conversions.convert(student.percentage, 'Integer')}" />
<td th:text="${iStat.index}" />
<td th:text="${iStat.count}" />
<td th:text="${iStat.first}" />
<td th:text="${iStat.last}" />
</tr>
</tbody>
</table>
<div>
<p>
<a th:href="@{/}" th:text="#{msg.Home}" />
</p>
</div>
</body>
</html>
@@ -1,50 +0,0 @@
<!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>