[BAEL-3392] Formatted code examples for [BAEL-3392]

This commit is contained in:
Martin van Wingerden
2019-11-01 09:05:12 +01:00
parent db85c8f275
commit 0e23f2e682
20519 changed files with 1642357 additions and 0 deletions
@@ -0,0 +1,11 @@
package com.baeldung.thymeleaf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,23 @@
package com.baeldung.thymeleaf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
@Configuration
public class ThymeleafConfig {
@Bean
public ClassLoaderTemplateResolver secondaryTemplateResolver() {
ClassLoaderTemplateResolver secondaryTemplateResolver = new ClassLoaderTemplateResolver();
secondaryTemplateResolver.setPrefix("templates-2/");
secondaryTemplateResolver.setSuffix(".html");
secondaryTemplateResolver.setTemplateMode(TemplateMode.HTML);
secondaryTemplateResolver.setCharacterEncoding("UTF-8");
secondaryTemplateResolver.setOrder(1);
secondaryTemplateResolver.setCheckExistence(true);
return secondaryTemplateResolver;
}
}
@@ -0,0 +1,28 @@
package com.baeldung.thymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import static java.util.Arrays.asList;
@Controller
public class ParticipantController {
@RequestMapping("/participants")
public String index(
@RequestParam(value = "participant", required = false) String participant,
@RequestParam(value = "country", required = false) String country,
@RequestParam(value = "action", required = false) String action,
@RequestParam(value = "id", required = false) Integer id,
Model model
) {
model.addAttribute("id", id);
List<Integer> userIds = asList(1,2,3,4);
model.addAttribute("userIds", userIds);
return "participants";
}
}
@@ -0,0 +1,22 @@
package com.baeldung.thymeleaf.enums;
public enum Color {
BLACK("Black"),
BLUE("Blue"),
RED("Red"),
YELLOW("Yellow"),
GREEN("Green"),
ORANGE("Orange"),
PURPLE("Purple"),
WHITE("White");
private final String displayValue;
private Color(String displayValue) {
this.displayValue = displayValue;
}
public String getDisplayValue() {
return displayValue;
}
}
@@ -0,0 +1,27 @@
package com.baeldung.thymeleaf.enums;
public class Widget {
private String name;
private Color color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
@Override
public String toString() {
return "Widget [name=" + name + ", color=" + color + "]";
}
}
@@ -0,0 +1,23 @@
package com.baeldung.thymeleaf.enums;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.ui.Model;
@Controller
public class WidgetController {
@GetMapping("/widget/add")
public String addWidget(@ModelAttribute Widget widget) {
return "enums/new";
}
@PostMapping("/widget/add")
public String saveWidget(@Valid Widget widget, Model model) {
model.addAttribute("widget", widget);
return "enums/view";
}
}
@@ -0,0 +1,64 @@
package com.baeldung.thymeleaf.lists;
import java.util.ArrayList;
import java.util.Comparator;
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.RequestMapping;
@Controller
@RequestMapping("/lists")
public class ListsController {
@GetMapping("/toList")
public String usingToList(Model model) {
List<String> colors = getColors();
String[] colorsArray = colors.toArray(new String[0]);
model.addAttribute("myArray", colorsArray);
return "lists/toList";
}
@GetMapping("/contains")
public String usingContains(Model model) {
model.addAttribute("myList", getColors());
model.addAttribute("others", getOtherColors());
return "lists/contains";
}
@GetMapping("/size")
public String usingSize(Model model) {
model.addAttribute("myList", getColors());
return "lists/size";
}
@GetMapping("/isEmpty")
public String usingIsEmpty(Model model) {
model.addAttribute("myList", getColors());
return "lists/isEmpty";
}
@GetMapping("/sort")
public String usingSort(Model model) {
model.addAttribute("myList", getColors());
model.addAttribute("reverse", Comparator.reverseOrder());
return "lists/sort";
}
private List<String> getColors() {
List<String> colors = new ArrayList<>();
colors.add("green");
colors.add("yellow");
colors.add("red");
colors.add("blue");
return colors;
}
private List<String> getOtherColors() {
List<String> colors = new ArrayList<>();
colors.add("green");
colors.add("blue");
return colors;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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";
}
}
@@ -0,0 +1,13 @@
package com.baeldung.thymeleaf.templatedir;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "hello";
}
}