JAVA-8281: split or move spring-thymeleaf module
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.thymeleaf.config;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
import javax.servlet.ServletRegistration.Dynamic;
|
||||
|
||||
/**
|
||||
* Java configuration file that is used for web application initialization
|
||||
*/
|
||||
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
public WebApp() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class<?>[] { WebMVCConfig.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customizeRegistration(final Dynamic registration) {
|
||||
super.customizeRegistration(registration);
|
||||
}
|
||||
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
package com.baeldung.thymeleaf.config;
|
||||
|
||||
import com.baeldung.thymeleaf.formatter.NameFormatter;
|
||||
import com.baeldung.thymeleaf.utils.ArrayUtil;
|
||||
import nz.net.ultraq.thymeleaf.LayoutDialect;
|
||||
import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Description;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;
|
||||
import org.thymeleaf.spring5.ISpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan({ "com.baeldung.thymeleaf" })
|
||||
/*
|
||||
Java configuration file that is used for Spring MVC and Thymeleaf
|
||||
configurations
|
||||
*/
|
||||
public class WebMVCConfig implements WebMvcConfigurer, ApplicationContextAware {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver htmlViewResolver() {
|
||||
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
|
||||
resolver.setTemplateEngine(templateEngine(htmlTemplateResolver()));
|
||||
resolver.setContentType("text/html");
|
||||
resolver.setCharacterEncoding("UTF-8");
|
||||
resolver.setViewNames(ArrayUtil.array("*.html"));
|
||||
return resolver;
|
||||
}
|
||||
|
||||
private ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
|
||||
SpringTemplateEngine engine = new SpringTemplateEngine();
|
||||
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
|
||||
engine.addDialect(new Java8TimeDialect());
|
||||
engine.setTemplateResolver(templateResolver);
|
||||
engine.setTemplateEngineMessageSource(messageSource());
|
||||
return engine;
|
||||
}
|
||||
|
||||
private ITemplateResolver htmlTemplateResolver() {
|
||||
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
|
||||
resolver.setApplicationContext(applicationContext);
|
||||
resolver.setPrefix("/WEB-INF/views/");
|
||||
resolver.setCacheable(false);
|
||||
resolver.setTemplateMode(TemplateMode.HTML);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Spring Message Resolver")
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("messages");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
|
||||
localeResolver.setDefaultLocale(new Locale("en"));
|
||||
return localeResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
|
||||
localeChangeInterceptor.setParamName("lang");
|
||||
return localeChangeInterceptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(localeChangeInterceptor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**", "/css/**")
|
||||
.addResourceLocations("/WEB-INF/resources/", "/WEB-INF/css/");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Description("Custom Conversion Service")
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addFormatter(new NameFormatter());
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Book;
|
||||
import com.baeldung.thymeleaf.service.BookService;
|
||||
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 java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@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";
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
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 java.text.DateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 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";
|
||||
}
|
||||
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Student;
|
||||
import com.baeldung.thymeleaf.utils.StudentUtils;
|
||||
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 javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 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";
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import com.baeldung.thymeleaf.utils.TeacherUtils;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
public class TeacherController {
|
||||
|
||||
@RequestMapping(value = "/listTeachers", method = RequestMethod.GET)
|
||||
public String getInfo(Model model) {
|
||||
model.addAttribute("teachers", TeacherUtils.buildTeachers());
|
||||
|
||||
return "listTeachers.html";
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.thymeleaf.formatter;
|
||||
|
||||
import org.springframework.format.Formatter;
|
||||
import org.thymeleaf.util.StringUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
*
|
||||
* Name formatter class that implements the Spring Formatter interface.
|
||||
* Formats a name(String) and return the value with spaces replaced by commas.
|
||||
*
|
||||
*/
|
||||
public class NameFormatter implements Formatter<String> {
|
||||
|
||||
@Override
|
||||
public String print(String input, Locale locale) {
|
||||
return formatName(input, locale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(String input, Locale locale) throws ParseException {
|
||||
return formatName(input, locale);
|
||||
}
|
||||
|
||||
private String formatName(String input, Locale locale) {
|
||||
return StringUtils.replace(input, " ", ",");
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.thymeleaf.model;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
*
|
||||
* Simple student POJO with few fields
|
||||
*
|
||||
*/
|
||||
public class Student implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -8582553475226281591L;
|
||||
|
||||
@NotNull(message = "Student ID is required.")
|
||||
@Min(value = 1000, message = "Student ID must be at least 4 digits.")
|
||||
private Integer id;
|
||||
|
||||
@NotNull(message = "Student name is required.")
|
||||
private String name;
|
||||
|
||||
@NotNull(message = "Student gender is required.")
|
||||
private Character gender;
|
||||
|
||||
private Float percentage;
|
||||
|
||||
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 Character getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Character gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public Float getPercentage() {
|
||||
return percentage;
|
||||
}
|
||||
|
||||
public void setPercentage(Float percentage) {
|
||||
this.percentage = percentage;
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.thymeleaf.model;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.thymeleaf.service;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Book;
|
||||
import com.baeldung.thymeleaf.utils.BookUtils;
|
||||
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 java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@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;
|
||||
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.thymeleaf.utils;
|
||||
|
||||
public class ArrayUtil {
|
||||
|
||||
public static String[] array(String... args) {
|
||||
return args;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.thymeleaf.utils;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Book;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.thymeleaf.utils;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Student;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class StudentUtils {
|
||||
|
||||
private static List<Student> students = new ArrayList<Student>();
|
||||
|
||||
public static List<Student> buildStudents() {
|
||||
if (students.isEmpty()) {
|
||||
Student student1 = new Student();
|
||||
student1.setId(1001);
|
||||
student1.setName("John Smith");
|
||||
student1.setGender('M');
|
||||
student1.setPercentage(Float.valueOf("80.45"));
|
||||
|
||||
students.add(student1);
|
||||
|
||||
Student student2 = new Student();
|
||||
student2.setId(1002);
|
||||
student2.setName("Jane Williams");
|
||||
student2.setGender('F');
|
||||
student2.setPercentage(Float.valueOf("60.25"));
|
||||
|
||||
students.add(student2);
|
||||
}
|
||||
|
||||
return students;
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.thymeleaf.utils;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Teacher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,13 @@
|
||||
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.ListBooks=List Books with paging
|
||||
msg.courses=Courses
|
||||
msg.skills=Skills
|
||||
msg.active=Active
|
||||
@@ -0,0 +1,45 @@
|
||||
<!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>
|
||||
@@ -0,0 +1,29 @@
|
||||
<!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>
|
||||
@@ -0,0 +1,58 @@
|
||||
<!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>
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<!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>
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.thymeleaf;
|
||||
|
||||
import com.baeldung.thymeleaf.config.WebApp;
|
||||
import com.baeldung.thymeleaf.config.WebMVCConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class })
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user