Skip to content

Commit 5edb9ac

Browse files
pxaiKevinGilmore
authored andcommitted
BAEL-1562 - Working with Fragments in Thymeleaf (eugenp#3868)
* BAEL-1562 - Thymeleaf sample working * BAEL-1562 Code added for Fragments sample * BAEL-1562 - Last correction for the test * BAEL-1562 - Thymeleaf sample working * BAEL-1562 Code added for Fragments sample * BAEL-1562 - Last correction for the test
1 parent b20a3a1 commit 5edb9ac

18 files changed

Lines changed: 573 additions & 315 deletions

File tree

‎spring-thymeleaf/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ http://localhost:8082/spring-thymeleaf/addStudent/
2727
http://localhost:8082/spring-thymeleaf/listStudents/
2828

2929
The first URL is the home page of the application. The home page has links to the other two pages.
30+
31+
### Security
32+
The user/password required is: user1/user1Pass

‎spring-thymeleaf/pom.xml‎

Lines changed: 209 additions & 209 deletions
Large diffs are not rendered by default.

‎spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ public ViewResolver plainViewResolver() {
7777
return resolver;
7878
}
7979

80-
private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
81-
SpringTemplateEngine engine = new SpringTemplateEngine();
82-
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
83-
engine.addDialect(new Java8TimeDialect());
84-
engine.setTemplateResolver(templateResolver);
85-
engine.setTemplateEngineMessageSource(messageSource());
86-
return engine;
87-
}
80+
private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
81+
SpringTemplateEngine engine = new SpringTemplateEngine();
82+
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
83+
engine.addDialect(new Java8TimeDialect());
84+
engine.setTemplateResolver(templateResolver);
85+
engine.setTemplateEngineMessageSource(messageSource());
86+
return engine;
87+
}
8888

8989
private ITemplateResolver htmlTemplateResolver() {
9090
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
@@ -142,7 +142,8 @@ public void addInterceptors(InterceptorRegistry registry) {
142142

143143
@Override
144144
public void addResourceHandlers(ResourceHandlerRegistry registry) {
145-
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
145+
registry.addResourceHandler("/resources/**", "/css/**")
146+
.addResourceLocations("/WEB-INF/resources/", "/WEB-INF/css/");
146147
}
147148

148149
@Override
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.thymeleaf.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
7+
import com.baeldung.thymeleaf.utils.StudentUtils;
8+
9+
@Controller
10+
public class FragmentsController {
11+
12+
@GetMapping("/fragments")
13+
public String getHome() {
14+
return "fragments.html";
15+
}
16+
17+
@GetMapping("/markup")
18+
public String markupPage() {
19+
return "markup.html";
20+
}
21+
22+
@GetMapping("/params")
23+
public String paramsPage() {
24+
return "params.html";
25+
}
26+
27+
@GetMapping("/other")
28+
public String otherPage(Model model) {
29+
model.addAttribute("data", StudentUtils.buildStudents());
30+
return "other.html";
31+
}
32+
33+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
msg.id=ID
2-
msg.name=Name
3-
msg.gender=Gender
4-
msg.percent=Percentage
5-
welcome.message=Welcome Student !!!
6-
msg.AddStudent=Add Student
7-
msg.ListStudents=List Students
8-
msg.Home=Home
9-
1+
msg.id=ID
2+
msg.name=Name
3+
msg.gender=Gender
4+
msg.percent=Percentage
5+
welcome.message=Welcome Student !!!
6+
msg.AddStudent=Add Student
7+
msg.ListStudents=List Students
8+
msg.Home=Home
9+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
background-color: lightGray
3+
}
4+
5+
.dark {
6+
background-color: black;
7+
color: white;
8+
}
9+
10+
.light {
11+
background-color: #f1f1f1;
12+
color: #ccc;
13+
}
Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
<!DOCTYPE html>
2-
<html xmlns="http://www.w3.org/1999/xhtml"
3-
xmlns:th="http://www.thymeleaf.org">
4-
<head>
5-
<title>Add Student</title>
6-
</head>
7-
<body>
8-
<h1>Add Student</h1>
9-
<form action="#" th:action="@{/saveStudent}" th:object="${student}"
10-
method="post">
11-
<ul>
12-
<li th:errors="*{id}" />
13-
<li th:errors="*{name}" />
14-
<li th:errors="*{gender}" />
15-
<li th:errors="*{percentage}" />
16-
</ul>
17-
<table border="1">
18-
<tr>
19-
<td><label th:text="#{msg.id}" /></td>
20-
<td><input type="number" th:field="*{id}" /></td>
21-
</tr>
22-
<tr>
23-
<td><label th:text="#{msg.name}" /></td>
24-
<td><input type="text" th:field="*{name}" /></td>
25-
</tr>
26-
<tr>
27-
<td><label th:text="#{msg.gender}" /></td>
28-
<td><select th:field="*{gender}">
29-
<option th:value="'M'" th:text="Male"></option>
30-
<option th:value="'F'" th:text="Female"></option>
31-
</select></td>
32-
</tr>
33-
<tr>
34-
<td><label th:text="#{msg.percent}" /></td>
35-
<td><input type="text" th:field="*{percentage}" /></td>
36-
</tr>
37-
<tr>
38-
<td><input type="submit" value="Submit" /></td>
39-
</tr>
40-
</table>
41-
</form>
42-
</body>
43-
</html>
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<head>
5+
<title>Add Student</title>
6+
</head>
7+
<body>
8+
<h1>Add Student</h1>
9+
<form action="#" th:action="@{/saveStudent}" th:object="${student}"
10+
method="post">
11+
<ul>
12+
<li th:errors="*{id}" />
13+
<li th:errors="*{name}" />
14+
<li th:errors="*{gender}" />
15+
<li th:errors="*{percentage}" />
16+
</ul>
17+
<table border="1">
18+
<tr>
19+
<td><label th:text="#{msg.id}" /></td>
20+
<td><input type="number" th:field="*{id}" /></td>
21+
</tr>
22+
<tr>
23+
<td><label th:text="#{msg.name}" /></td>
24+
<td><input type="text" th:field="*{name}" /></td>
25+
</tr>
26+
<tr>
27+
<td><label th:text="#{msg.gender}" /></td>
28+
<td><select th:field="*{gender}">
29+
<option th:value="'M'" th:text="Male"></option>
30+
<option th:value="'F'" th:text="Female"></option>
31+
</select></td>
32+
</tr>
33+
<tr>
34+
<td><label th:text="#{msg.percent}" /></td>
35+
<td><input type="text" th:field="*{percentage}" /></td>
36+
</tr>
37+
<tr>
38+
<td><input type="submit" value="Submit" /></td>
39+
</tr>
40+
</table>
41+
</form>
42+
</body>
43+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Thymeleaf Fragments: home</title>
5+
<!--/*/ <th:block th:include="fragments/general.html :: headerfiles"></th:block> /*/-->
6+
</head>
7+
<body>
8+
<header th:insert="fragments/general.html :: header"> </header>
9+
<p>Go to the next page to see fragments in action</p>
10+
<div th:replace="fragments/general.html :: footer"></div>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div th:fragment="formField (field, value, size)">
2+
<div>
3+
<label th:for="${#strings.toLowerCase(field)}"> <span
4+
th:text="${field}">Field</span>
5+
</label>
6+
</div>
7+
<div>
8+
<input type="text" th:id="${#strings.toLowerCase(field)}"
9+
th:name="${#strings.toLowerCase(field)}" th:value="${value}"
10+
th:size="${size}">
11+
</div>
12+
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head th:fragment="headerfiles">
4+
<meta charset="UTF-8" />
5+
<link th:href="@{/css/styles.css}" rel="stylesheet">
6+
</head>
7+
<body>
8+
<div th:fragment="header">
9+
<h1>Thymeleaf Fragments sample</h1>
10+
</div>
11+
<p>Go to the next page to see fragments in action</p>
12+
<aside>
13+
<div>This is a sidebar</div>
14+
</aside>
15+
<div class="another">This is another sidebar</div>
16+
<footer th:fragment="footer">
17+
<a th:href="@{/fragments}">Fragments Index</a> |
18+
<a th:href="@{/markup}">Markup inclussion</a> |
19+
<a th:href="@{/params}">Fragment params</a> |
20+
<a th:href="@{/other}">Other</a>
21+
</footer>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)