Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0286635

Browse filesBrowse files
amdegregoriomaibin
authored andcommitted
BAEL-3013 (eugenp#8020)
1 parent 518b50e commit 0286635
Copy full SHA for 0286635

File tree

Expand file treeCollapse file tree

5 files changed

+170
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+170
-0
lines changed
Open diff view settings
Collapse file
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.thymeleaf.pathvariables;
2+
3+
public class Detail {
4+
private int id;
5+
private String description;
6+
7+
public Detail(int id, String description) {
8+
super();
9+
this.id = id;
10+
this.description = description;
11+
}
12+
13+
public int getId() {
14+
return id;
15+
}
16+
17+
public void setId(int id) {
18+
this.id = id;
19+
}
20+
21+
public String getDescription() {
22+
return description;
23+
}
24+
25+
public void setDescription(String description) {
26+
this.description = description;
27+
}
28+
}
Collapse file
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.thymeleaf.pathvariables;
2+
3+
import java.util.List;
4+
5+
public class Item {
6+
private int id;
7+
private String name;
8+
private List<Detail> details;
9+
10+
public Item(int id, String name) {
11+
super();
12+
this.id = id;
13+
this.name = name;
14+
}
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public List<Detail> getDetails() {
33+
return details;
34+
}
35+
36+
public void setDetails(List<Detail> details) {
37+
this.details = details;
38+
}
39+
}
Collapse file
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.thymeleaf.pathvariables;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
11+
@Controller
12+
public class PathVariablesController {
13+
private List<Item> items = new ArrayList<Item>();
14+
15+
public PathVariablesController() {
16+
Item item1 = new Item(1, "First Item");
17+
List<Detail> item1Details = new ArrayList<>();
18+
item1Details.add(new Detail(1, "Green"));
19+
item1Details.add(new Detail(2, "Large"));
20+
item1.setDetails(item1Details);
21+
items.add(item1);
22+
23+
Item item2 = new Item(2, "Second Item");
24+
List<Detail> item2Details = new ArrayList<>();
25+
item2Details.add(new Detail(1, "Red"));
26+
item2Details.add(new Detail(2, "Medium"));
27+
item2.setDetails(item2Details);
28+
items.add(item2);
29+
}
30+
31+
@GetMapping("/pathvars")
32+
public String start(Model model) {
33+
model.addAttribute("items", items);
34+
return "pathvariables/index";
35+
}
36+
37+
@GetMapping("/pathvars/single/{id}")
38+
public String singlePathVariable(@PathVariable("id") int id, Model model) {
39+
if (id == 1) {
40+
model.addAttribute("item", new Item(1, "First Item"));
41+
} else {
42+
model.addAttribute("item", new Item(2, "Second Item"));
43+
}
44+
45+
return "pathvariables/view";
46+
}
47+
48+
@GetMapping("/pathvars/item/{itemId}/detail/{detailId}")
49+
public String multiplePathVariable(@PathVariable("itemId") int itemId, @PathVariable("detailId") int detailId, Model model) {
50+
for (Item item : items) {
51+
if (item.getId() == itemId) {
52+
model.addAttribute("item", item);
53+
for (Detail detail : item.getDetails()) {
54+
if (detail.getId() == detailId) {
55+
model.addAttribute("detail", detail);
56+
}
57+
}
58+
}
59+
}
60+
return "pathvariables/view";
61+
}
62+
}
Collapse file
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>PathVariables in Thymeleaf</title>
6+
</head>
7+
<body>
8+
<h3>Items</h3>
9+
<div th:each="item : ${items}">
10+
<a th:href="@{/pathvars/single/{id}(id = ${item.id})}">
11+
<span th:text="${item.name}"></span>
12+
</a>
13+
<ul>
14+
<li th:each="detail : ${item.details}">
15+
<a th:href="@{/pathvars/item/{itemId}/detail/{detailId}(itemId = ${item.id}, detailId = ${detail.id})}">
16+
<span th:text="${detail.description}"></span>
17+
</a>
18+
</li>
19+
</ul>
20+
</div>
21+
</body>
22+
</html>
Collapse file
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Pathvariables in Thymeleaf</title>
6+
</head>
7+
<body>
8+
<h3>View Item</h3>
9+
<div>
10+
<label for="name">Name: </label>
11+
<span th:text="${item.name}"></span>
12+
</div>
13+
<h4 th:if="${detail}">Detail</h4>
14+
<div th:if="${detail}">
15+
<label for="description">Description: </label>
16+
<span th:text="${detail.description}"></span>
17+
</div>
18+
</body>
19+
</html>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.