Skip to content

Commit 00bcc42

Browse files
author
chaos2418
committed
JAVA-8281: split or move spring-thymeleaf module
1 parent 14e93c1 commit 00bcc42

File tree

28 files changed

+517
-43
lines changed

28 files changed

+517
-43
lines changed

spring-web-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<module>spring-thymeleaf</module>
4545
<module>spring-thymeleaf-2</module>
4646
<module>spring-thymeleaf-3</module>
47+
<module>spring-thymeleaf-4</module>
4748
<module>spring-boot-jsp</module>
4849
<module>spring-web-url</module>
4950
</modules>

spring-web-modules/spring-thymeleaf-3/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ This module contains articles about Spring with Thymeleaf
1111
- [Using Hidden Inputs with Spring and Thymeleaf](https://www.baeldung.com/spring-thymeleaf-hidden-inputs)
1212
- [Thymeleaf Variables](https://www.baeldung.com/thymeleaf-variables)
1313
- [Displaying Error Messages with Thymeleaf in Spring](https://www.baeldung.com/spring-thymeleaf-error-messages)
14+
- [[next -->]](/spring-thymeleaf-4)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Spring Thymeleaf
2+
3+
This module contains articles about Spring with Thymeleaf
4+
5+
### Relevant Articles:
6+
- [Conditionals in Thymeleaf](https://www.baeldung.com/spring-thymeleaf-conditionals)
7+
- [Iteration in Thymeleaf](https://www.baeldung.com/thymeleaf-iteration)
8+
- [Spring with Thymeleaf Pagination for a List](https://www.baeldung.com/spring-thymeleaf-pagination)
9+
10+
### Build the Project
11+
12+
mvn clean install
13+
14+
### Run the Project
15+
16+
mvn cargo:run
17+
- **note**: starts on port '8082'
18+
19+
Access the pages using the URLs:
20+
21+
- http://localhost:8082/spring-thymeleaf-4/
22+
- http://localhost:8082/spring-thymeleaf-4/addStudent/
23+
- http://localhost:8082/spring-thymeleaf-4/listStudents/
24+
25+
The first URL is the home page of the application. The home page has links to the second and third pages.
26+
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>spring-thymeleaf-4</artifactId>
7+
<name>spring-thymeleaf-4</name>
8+
<packaging>war</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-spring-5</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../../parent-spring-5</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<!-- Spring -->
19+
<dependency>
20+
<groupId>org.springframework</groupId>
21+
<artifactId>spring-context</artifactId>
22+
<version>${spring.version}</version>
23+
<exclusions>
24+
<!-- Exclude Commons Logging in favor of SLF4j -->
25+
<exclusion>
26+
<groupId>commons-logging</groupId>
27+
<artifactId>commons-logging</artifactId>
28+
</exclusion>
29+
</exclusions>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework</groupId>
33+
<artifactId>spring-webmvc</artifactId>
34+
<version>${spring.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.data</groupId>
38+
<artifactId>spring-data-commons</artifactId>
39+
<version>${spring-data.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>javax.validation</groupId>
43+
<artifactId>validation-api</artifactId>
44+
<version>${javax.validation-version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.hibernate.validator</groupId>
48+
<artifactId>hibernate-validator</artifactId>
49+
<version>${hibernate-validator.version}</version>
50+
</dependency>
51+
<!-- Thymeleaf -->
52+
<dependency>
53+
<groupId>org.thymeleaf</groupId>
54+
<artifactId>thymeleaf</artifactId>
55+
<version>${org.thymeleaf-version}</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.thymeleaf</groupId>
59+
<artifactId>thymeleaf-spring5</artifactId>
60+
<version>${org.thymeleaf-version}</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>nz.net.ultraq.thymeleaf</groupId>
64+
<artifactId>thymeleaf-layout-dialect</artifactId>
65+
<version>${thymeleaf-layout-dialect.version}</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.thymeleaf.extras</groupId>
69+
<artifactId>thymeleaf-extras-java8time</artifactId>
70+
<version>${org.thymeleaf.extras-version}</version>
71+
</dependency>
72+
<!-- Servlet -->
73+
<dependency>
74+
<groupId>javax.servlet</groupId>
75+
<artifactId>javax.servlet-api</artifactId>
76+
<version>${javax.servlet-api.version}</version>
77+
<scope>provided</scope>
78+
</dependency>
79+
<!-- test scoped -->
80+
<dependency>
81+
<groupId>org.springframework</groupId>
82+
<artifactId>spring-test</artifactId>
83+
<version>${spring.version}</version>
84+
<scope>test</scope>
85+
</dependency>
86+
</dependencies>
87+
88+
<build>
89+
<plugins>
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-war-plugin</artifactId>
93+
<version>${maven-war-plugin.version}</version>
94+
<configuration>
95+
<failOnMissingWebXml>false</failOnMissingWebXml>
96+
</configuration>
97+
</plugin>
98+
<plugin>
99+
<groupId>org.codehaus.cargo</groupId>
100+
<artifactId>cargo-maven2-plugin</artifactId>
101+
<version>${cargo-maven2-plugin.version}</version>
102+
<configuration>
103+
<wait>true</wait>
104+
<container>
105+
<containerId>jetty9x</containerId>
106+
<type>embedded</type>
107+
<systemProperties>
108+
</systemProperties>
109+
</container>
110+
<configuration>
111+
<properties>
112+
<cargo.servlet.port>8082</cargo.servlet.port>
113+
</properties>
114+
</configuration>
115+
</configuration>
116+
</plugin>
117+
</plugins>
118+
</build>
119+
120+
<properties>
121+
<spring-data.version>2.3.2.RELEASE</spring-data.version>
122+
<org.thymeleaf-version>3.0.11.RELEASE</org.thymeleaf-version>
123+
<org.thymeleaf.extras-version>3.0.4.RELEASE</org.thymeleaf.extras-version>
124+
<thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
125+
<javax.validation-version>2.0.1.Final</javax.validation-version>
126+
<hibernate-validator.version>6.0.11.Final</hibernate-validator.version>
127+
<!-- Maven plugins -->
128+
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
129+
</properties>
130+
131+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.thymeleaf.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
import javax.servlet.ServletRegistration.Dynamic;
6+
7+
/**
8+
* Java configuration file that is used for web application initialization
9+
*/
10+
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
11+
12+
public WebApp() {
13+
super();
14+
}
15+
16+
@Override
17+
protected Class<?>[] getRootConfigClasses() {
18+
return null;
19+
}
20+
21+
@Override
22+
protected Class<?>[] getServletConfigClasses() {
23+
return new Class<?>[] { WebMVCConfig.class };
24+
}
25+
26+
@Override
27+
protected String[] getServletMappings() {
28+
return new String[] { "/" };
29+
}
30+
31+
@Override
32+
protected void customizeRegistration(final Dynamic registration) {
33+
super.customizeRegistration(registration);
34+
}
35+
36+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.baeldung.thymeleaf.config;
2+
3+
import com.baeldung.thymeleaf.formatter.NameFormatter;
4+
import com.baeldung.thymeleaf.utils.ArrayUtil;
5+
import nz.net.ultraq.thymeleaf.LayoutDialect;
6+
import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.ApplicationContextAware;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.ComponentScan;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.context.annotation.Description;
13+
import org.springframework.context.support.ResourceBundleMessageSource;
14+
import org.springframework.format.FormatterRegistry;
15+
import org.springframework.web.servlet.LocaleResolver;
16+
import org.springframework.web.servlet.ViewResolver;
17+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
18+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
19+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
20+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
21+
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
22+
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
23+
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;
24+
import org.thymeleaf.spring5.ISpringTemplateEngine;
25+
import org.thymeleaf.spring5.SpringTemplateEngine;
26+
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
27+
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
28+
import org.thymeleaf.templatemode.TemplateMode;
29+
import org.thymeleaf.templateresolver.ITemplateResolver;
30+
31+
import java.util.Locale;
32+
33+
@Configuration
34+
@EnableWebMvc
35+
@ComponentScan({ "com.baeldung.thymeleaf" })
36+
/*
37+
Java configuration file that is used for Spring MVC and Thymeleaf
38+
configurations
39+
*/
40+
public class WebMVCConfig implements WebMvcConfigurer, ApplicationContextAware {
41+
42+
private ApplicationContext applicationContext;
43+
44+
@Override
45+
public void setApplicationContext(ApplicationContext applicationContext) {
46+
this.applicationContext = applicationContext;
47+
}
48+
49+
@Bean
50+
public ViewResolver htmlViewResolver() {
51+
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
52+
resolver.setTemplateEngine(templateEngine(htmlTemplateResolver()));
53+
resolver.setContentType("text/html");
54+
resolver.setCharacterEncoding("UTF-8");
55+
resolver.setViewNames(ArrayUtil.array("*.html"));
56+
return resolver;
57+
}
58+
59+
private ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
60+
SpringTemplateEngine engine = new SpringTemplateEngine();
61+
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
62+
engine.addDialect(new Java8TimeDialect());
63+
engine.setTemplateResolver(templateResolver);
64+
engine.setTemplateEngineMessageSource(messageSource());
65+
return engine;
66+
}
67+
68+
private ITemplateResolver htmlTemplateResolver() {
69+
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
70+
resolver.setApplicationContext(applicationContext);
71+
resolver.setPrefix("/WEB-INF/views/");
72+
resolver.setCacheable(false);
73+
resolver.setTemplateMode(TemplateMode.HTML);
74+
return resolver;
75+
}
76+
77+
@Bean
78+
@Description("Spring Message Resolver")
79+
public ResourceBundleMessageSource messageSource() {
80+
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
81+
messageSource.setBasename("messages");
82+
return messageSource;
83+
}
84+
85+
@Bean
86+
public LocaleResolver localeResolver() {
87+
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
88+
localeResolver.setDefaultLocale(new Locale("en"));
89+
return localeResolver;
90+
}
91+
92+
@Bean
93+
public LocaleChangeInterceptor localeChangeInterceptor() {
94+
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
95+
localeChangeInterceptor.setParamName("lang");
96+
return localeChangeInterceptor;
97+
}
98+
99+
@Override
100+
public void addInterceptors(InterceptorRegistry registry) {
101+
registry.addInterceptor(localeChangeInterceptor());
102+
}
103+
104+
@Override
105+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
106+
registry.addResourceHandler("/resources/**", "/css/**")
107+
.addResourceLocations("/WEB-INF/resources/", "/WEB-INF/css/");
108+
}
109+
110+
@Override
111+
@Description("Custom Conversion Service")
112+
public void addFormatters(FormatterRegistry registry) {
113+
registry.addFormatter(new NameFormatter());
114+
}
115+
}

spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/BookController.java renamed to spring-web-modules/spring-thymeleaf-4/src/main/java/com/baeldung/thymeleaf/controller/BookController.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package com.baeldung.thymeleaf.controller;
22

3-
import java.util.List;
4-
import java.util.Optional;
5-
import java.util.stream.Collectors;
6-
import java.util.stream.IntStream;
7-
3+
import com.baeldung.thymeleaf.model.Book;
4+
import com.baeldung.thymeleaf.service.BookService;
85
import org.springframework.beans.factory.annotation.Autowired;
96
import org.springframework.data.domain.Page;
107
import org.springframework.data.domain.PageRequest;
11-
128
import org.springframework.stereotype.Controller;
139
import org.springframework.ui.Model;
1410
import org.springframework.web.bind.annotation.RequestMapping;
1511
import org.springframework.web.bind.annotation.RequestMethod;
1612
import org.springframework.web.bind.annotation.RequestParam;
1713

18-
import com.baeldung.thymeleaf.model.Book;
19-
import com.baeldung.thymeleaf.service.BookService;
14+
import java.util.List;
15+
import java.util.Optional;
16+
import java.util.stream.Collectors;
17+
import java.util.stream.IntStream;
2018

2119
@Controller
2220
public class BookController {

spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java renamed to spring-web-modules/spring-thymeleaf-4/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.baeldung.thymeleaf.controller;
22

3-
import java.text.DateFormat;
4-
import java.util.Date;
5-
import java.util.Locale;
6-
73
import org.springframework.stereotype.Controller;
84
import org.springframework.ui.Model;
95
import org.springframework.web.bind.annotation.RequestMapping;
106
import org.springframework.web.bind.annotation.RequestMethod;
117

8+
import java.text.DateFormat;
9+
import java.util.Date;
10+
import java.util.Locale;
11+
1212
/**
1313
* Handles requests for the application home page.
1414
*

spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/StudentController.java renamed to spring-web-modules/spring-thymeleaf-4/src/main/java/com/baeldung/thymeleaf/controller/StudentController.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package com.baeldung.thymeleaf.controller;
22

3-
import java.util.List;
4-
5-
import javax.validation.Valid;
6-
3+
import com.baeldung.thymeleaf.model.Student;
4+
import com.baeldung.thymeleaf.utils.StudentUtils;
75
import org.springframework.stereotype.Controller;
86
import org.springframework.ui.Model;
97
import org.springframework.validation.BindingResult;
108
import org.springframework.web.bind.annotation.ModelAttribute;
119
import org.springframework.web.bind.annotation.RequestMapping;
1210
import org.springframework.web.bind.annotation.RequestMethod;
1311

14-
import com.baeldung.thymeleaf.model.Student;
15-
import com.baeldung.thymeleaf.utils.StudentUtils;
12+
import javax.validation.Valid;
13+
import java.util.List;
1614

1715
/**
1816
* Handles requests for the student model.

0 commit comments

Comments
 (0)