This commit is contained in:
Amy DeGregorio
2019-10-14 12:06:25 -04:00
committed by maibin
parent 518b50ee98
commit 0286635f39
5 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
package com.baeldung.thymeleaf.pathvariables;
public class Detail {
private int id;
private String description;
public Detail(int id, String description) {
super();
this.id = id;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@@ -0,0 +1,39 @@
package com.baeldung.thymeleaf.pathvariables;
import java.util.List;
public class Item {
private int id;
private String name;
private List<Detail> details;
public Item(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;
}
public List<Detail> getDetails() {
return details;
}
public void setDetails(List<Detail> details) {
this.details = details;
}
}

View File

@@ -0,0 +1,62 @@
package com.baeldung.thymeleaf.pathvariables;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class PathVariablesController {
private List<Item> items = new ArrayList<Item>();
public PathVariablesController() {
Item item1 = new Item(1, "First Item");
List<Detail> item1Details = new ArrayList<>();
item1Details.add(new Detail(1, "Green"));
item1Details.add(new Detail(2, "Large"));
item1.setDetails(item1Details);
items.add(item1);
Item item2 = new Item(2, "Second Item");
List<Detail> item2Details = new ArrayList<>();
item2Details.add(new Detail(1, "Red"));
item2Details.add(new Detail(2, "Medium"));
item2.setDetails(item2Details);
items.add(item2);
}
@GetMapping("/pathvars")
public String start(Model model) {
model.addAttribute("items", items);
return "pathvariables/index";
}
@GetMapping("/pathvars/single/{id}")
public String singlePathVariable(@PathVariable("id") int id, Model model) {
if (id == 1) {
model.addAttribute("item", new Item(1, "First Item"));
} else {
model.addAttribute("item", new Item(2, "Second Item"));
}
return "pathvariables/view";
}
@GetMapping("/pathvars/item/{itemId}/detail/{detailId}")
public String multiplePathVariable(@PathVariable("itemId") int itemId, @PathVariable("detailId") int detailId, Model model) {
for (Item item : items) {
if (item.getId() == itemId) {
model.addAttribute("item", item);
for (Detail detail : item.getDetails()) {
if (detail.getId() == detailId) {
model.addAttribute("detail", detail);
}
}
}
}
return "pathvariables/view";
}
}

View File

@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>PathVariables in Thymeleaf</title>
</head>
<body>
<h3>Items</h3>
<div th:each="item : ${items}">
<a th:href="@{/pathvars/single/{id}(id = ${item.id})}">
<span th:text="${item.name}"></span>
</a>
<ul>
<li th:each="detail : ${item.details}">
<a th:href="@{/pathvars/item/{itemId}/detail/{detailId}(itemId = ${item.id}, detailId = ${detail.id})}">
<span th:text="${detail.description}"></span>
</a>
</li>
</ul>
</div>
</body>
</html>

View File

@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Pathvariables in Thymeleaf</title>
</head>
<body>
<h3>View Item</h3>
<div>
<label for="name">Name: </label>
<span th:text="${item.name}"></span>
</div>
<h4 th:if="${detail}">Detail</h4>
<div th:if="${detail}">
<label for="description">Description: </label>
<span th:text="${detail.description}"></span>
</div>
</body>
</html>