Rename package to conform with site name
This commit is contained in:
parent
4192ee3f75
commit
bb321982a1
|
@ -1,7 +1,7 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-thymeleaf</artifactId>
|
<artifactId>spring-thymeleaf</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
|
@ -1,36 +1,36 @@
|
||||||
package org.baeldung.thymeleaf.config;
|
package com.baeldung.thymeleaf.config;
|
||||||
|
|
||||||
import javax.servlet.ServletRegistration.Dynamic;
|
import javax.servlet.ServletRegistration.Dynamic;
|
||||||
|
|
||||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Java configuration file that is used for web application initialization
|
* Java configuration file that is used for web application initialization
|
||||||
*/
|
*/
|
||||||
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
|
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||||
|
|
||||||
public WebApp() {
|
public WebApp() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?>[] getRootConfigClasses() {
|
protected Class<?>[] getRootConfigClasses() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?>[] getServletConfigClasses() {
|
protected Class<?>[] getServletConfigClasses() {
|
||||||
return new Class<?>[] { WebMVCConfig.class };
|
return new Class<?>[] { WebMVCConfig.class };
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String[] getServletMappings() {
|
protected String[] getServletMappings() {
|
||||||
return new String[] { "/" };
|
return new String[] { "/" };
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void customizeRegistration(final Dynamic registration) {
|
protected void customizeRegistration(final Dynamic registration) {
|
||||||
super.customizeRegistration(registration);
|
super.customizeRegistration(registration);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,73 +1,73 @@
|
||||||
package org.baeldung.thymeleaf.config;
|
package com.baeldung.thymeleaf.config;
|
||||||
|
|
||||||
import org.baeldung.thymeleaf.formatter.NameFormatter;
|
import com.baeldung.thymeleaf.formatter.NameFormatter;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Description;
|
import org.springframework.context.annotation.Description;
|
||||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||||
import org.springframework.format.FormatterRegistry;
|
import org.springframework.format.FormatterRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||||
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
@ComponentScan({ "org.baeldung.thymeleaf" })
|
@ComponentScan({ "com.baeldung.thymeleaf" })
|
||||||
/**
|
/**
|
||||||
* Java configuration file that is used for Spring MVC and Thymeleaf
|
* Java configuration file that is used for Spring MVC and Thymeleaf
|
||||||
* configurations
|
* configurations
|
||||||
*/
|
*/
|
||||||
public class WebMVCConfig extends WebMvcConfigurerAdapter {
|
public class WebMVCConfig extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Description("Thymeleaf Template Resolver")
|
@Description("Thymeleaf Template Resolver")
|
||||||
public ServletContextTemplateResolver templateResolver() {
|
public ServletContextTemplateResolver templateResolver() {
|
||||||
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
||||||
templateResolver.setPrefix("/WEB-INF/views/");
|
templateResolver.setPrefix("/WEB-INF/views/");
|
||||||
templateResolver.setSuffix(".html");
|
templateResolver.setSuffix(".html");
|
||||||
templateResolver.setTemplateMode("HTML5");
|
templateResolver.setTemplateMode("HTML5");
|
||||||
|
|
||||||
return templateResolver;
|
return templateResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Description("Thymeleaf Template Engine")
|
@Description("Thymeleaf Template Engine")
|
||||||
public SpringTemplateEngine templateEngine() {
|
public SpringTemplateEngine templateEngine() {
|
||||||
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||||
templateEngine.setTemplateResolver(templateResolver());
|
templateEngine.setTemplateResolver(templateResolver());
|
||||||
|
|
||||||
return templateEngine;
|
return templateEngine;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Description("Thymeleaf View Resolver")
|
@Description("Thymeleaf View Resolver")
|
||||||
public ThymeleafViewResolver viewResolver() {
|
public ThymeleafViewResolver viewResolver() {
|
||||||
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||||
viewResolver.setTemplateEngine(templateEngine());
|
viewResolver.setTemplateEngine(templateEngine());
|
||||||
viewResolver.setOrder(1);
|
viewResolver.setOrder(1);
|
||||||
return viewResolver;
|
return viewResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Description("Spring Message Resolver")
|
@Description("Spring Message Resolver")
|
||||||
public ResourceBundleMessageSource messageSource() {
|
public ResourceBundleMessageSource messageSource() {
|
||||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||||
messageSource.setBasename("messages");
|
messageSource.setBasename("messages");
|
||||||
return messageSource;
|
return messageSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
|
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Description("Custom Conversion Service")
|
@Description("Custom Conversion Service")
|
||||||
public void addFormatters(FormatterRegistry registry) {
|
public void addFormatters(FormatterRegistry registry) {
|
||||||
registry.addFormatter(new NameFormatter());
|
registry.addFormatter(new NameFormatter());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.thymeleaf.controller;
|
package com.baeldung.thymeleaf.controller;
|
||||||
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
|
@ -1,70 +1,69 @@
|
||||||
package org.baeldung.thymeleaf.controller;
|
package com.baeldung.thymeleaf.controller;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import com.baeldung.thymeleaf.model.Student;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.baeldung.thymeleaf.model.Student;
|
|
||||||
|
/**
|
||||||
/**
|
* Handles requests for the student model.
|
||||||
* Handles requests for the student model.
|
*
|
||||||
*
|
*/
|
||||||
*/
|
@Controller
|
||||||
@Controller
|
public class StudentController {
|
||||||
public class StudentController {
|
|
||||||
|
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
|
||||||
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
|
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
|
||||||
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
|
if (!errors.hasErrors()) {
|
||||||
if (!errors.hasErrors()) {
|
// get mock objects
|
||||||
// get mock objects
|
List<Student> students = buildStudents();
|
||||||
List<Student> students = buildStudents();
|
// add current student
|
||||||
// add current student
|
students.add(student);
|
||||||
students.add(student);
|
model.addAttribute("students", students);
|
||||||
model.addAttribute("students", students);
|
}
|
||||||
}
|
return ((errors.hasErrors()) ? "addStudent" : "listStudents");
|
||||||
return ((errors.hasErrors()) ? "addStudent" : "listStudents");
|
}
|
||||||
}
|
|
||||||
|
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
|
||||||
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
|
public String addStudent(Model model) {
|
||||||
public String addStudent(Model model) {
|
model.addAttribute("student", new Student());
|
||||||
model.addAttribute("student", new Student());
|
return "addStudent";
|
||||||
return "addStudent";
|
}
|
||||||
}
|
|
||||||
|
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
|
||||||
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
|
public String listStudent(Model model) {
|
||||||
public String listStudent(Model model) {
|
|
||||||
|
model.addAttribute("students", buildStudents());
|
||||||
model.addAttribute("students", buildStudents());
|
|
||||||
|
return "listStudents";
|
||||||
return "listStudents";
|
}
|
||||||
}
|
|
||||||
|
private List<Student> buildStudents() {
|
||||||
private List<Student> buildStudents() {
|
List<Student> students = new ArrayList<Student>();
|
||||||
List<Student> students = new ArrayList<Student>();
|
|
||||||
|
Student student1 = new Student();
|
||||||
Student student1 = new Student();
|
student1.setId(1001);
|
||||||
student1.setId(1001);
|
student1.setName("John Smith");
|
||||||
student1.setName("John Smith");
|
student1.setGender('M');
|
||||||
student1.setGender('M');
|
student1.setPercentage(Float.valueOf("80.45"));
|
||||||
student1.setPercentage(Float.valueOf("80.45"));
|
|
||||||
|
students.add(student1);
|
||||||
students.add(student1);
|
|
||||||
|
Student student2 = new Student();
|
||||||
Student student2 = new Student();
|
student2.setId(1002);
|
||||||
student2.setId(1002);
|
student2.setName("Jane Williams");
|
||||||
student2.setName("Jane Williams");
|
student2.setGender('F');
|
||||||
student2.setGender('F');
|
student2.setPercentage(Float.valueOf("60.25"));
|
||||||
student2.setPercentage(Float.valueOf("60.25"));
|
|
||||||
|
students.add(student2);
|
||||||
students.add(student2);
|
return students;
|
||||||
return students;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
|
@ -1,30 +1,30 @@
|
||||||
package org.baeldung.thymeleaf.formatter;
|
package com.baeldung.thymeleaf.formatter;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.springframework.format.Formatter;
|
import org.springframework.format.Formatter;
|
||||||
import org.thymeleaf.util.StringUtils;
|
import org.thymeleaf.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Name formatter class that implements the Spring Formatter interface.
|
* Name formatter class that implements the Spring Formatter interface.
|
||||||
* Formats a name(String) and return the value with spaces replaced by commas.
|
* Formats a name(String) and return the value with spaces replaced by commas.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class NameFormatter implements Formatter<String> {
|
public class NameFormatter implements Formatter<String> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String print(String input, Locale locale) {
|
public String print(String input, Locale locale) {
|
||||||
return formatName(input, locale);
|
return formatName(input, locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String parse(String input, Locale locale) throws ParseException {
|
public String parse(String input, Locale locale) throws ParseException {
|
||||||
return formatName(input, locale);
|
return formatName(input, locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String formatName(String input, Locale locale) {
|
private String formatName(String input, Locale locale) {
|
||||||
return StringUtils.replace(input, " ", ",");
|
return StringUtils.replace(input, " ", ",");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,60 +1,60 @@
|
||||||
package org.baeldung.thymeleaf.model;
|
package com.baeldung.thymeleaf.model;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Simple student POJO with few fields
|
* Simple student POJO with few fields
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Student implements Serializable {
|
public class Student implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = -8582553475226281591L;
|
private static final long serialVersionUID = -8582553475226281591L;
|
||||||
|
|
||||||
@NotNull(message = "Student ID is required.")
|
@NotNull(message = "Student ID is required.")
|
||||||
@Min(value = 1000, message = "Student ID must be at least 4 digits.")
|
@Min(value = 1000, message = "Student ID must be at least 4 digits.")
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
@NotNull(message = "Student name is required.")
|
@NotNull(message = "Student name is required.")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@NotNull(message = "Student gender is required.")
|
@NotNull(message = "Student gender is required.")
|
||||||
private Character gender;
|
private Character gender;
|
||||||
|
|
||||||
private Float percentage;
|
private Float percentage;
|
||||||
|
|
||||||
public Integer getId() {
|
public Integer getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(Integer id) {
|
public void setId(Integer id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Character getGender() {
|
public Character getGender() {
|
||||||
return gender;
|
return gender;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGender(Character gender) {
|
public void setGender(Character gender) {
|
||||||
this.gender = gender;
|
this.gender = gender;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Float getPercentage() {
|
public Float getPercentage() {
|
||||||
return percentage;
|
return percentage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPercentage(Float percentage) {
|
public void setPercentage(Float percentage) {
|
||||||
this.percentage = percentage;
|
this.percentage = percentage;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue