JAVA-3544: Move spring-thymeleaf-2 into spring-web-modules
This commit is contained in:
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
+23
@@ -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;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.thymeleaf.arrays;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class ThymeleafArrayController {
|
||||
@GetMapping("/arrays")
|
||||
public String arrayController(Model model) {
|
||||
String[] continents = {"Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "Sourth America"};
|
||||
|
||||
model.addAttribute("continents", continents);
|
||||
|
||||
return "continents";
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.thymeleaf.booleanexpressions;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
/**
|
||||
* Controller to test boolean expressions
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
public class BooleanExpressionsController {
|
||||
|
||||
@RequestMapping(value = "/booleans", method = RequestMethod.GET)
|
||||
public String getDates(Model model) {
|
||||
// "truthy" values
|
||||
model.addAttribute("trueValue", true);
|
||||
model.addAttribute("one", 1);
|
||||
model.addAttribute("nonZeroCharacter", 'a');
|
||||
model.addAttribute("emptyString", "");
|
||||
model.addAttribute("foo", "foo");
|
||||
model.addAttribute("object", new Object());
|
||||
model.addAttribute("arrayOfZeros", new Integer[] { 0, 0 });
|
||||
model.addAttribute("arrayOfZeroAndOne", new Integer[] { 0, 1 });
|
||||
model.addAttribute("arrayOfOnes", new Integer[] { 1, 1 });
|
||||
|
||||
// "falsy" values
|
||||
model.addAttribute("nullValue", null);
|
||||
model.addAttribute("falseValue", false);
|
||||
model.addAttribute("zero", 0);
|
||||
model.addAttribute("zeroCharacter", '\0');
|
||||
model.addAttribute("falseString", "false");
|
||||
model.addAttribute("no", "no");
|
||||
model.addAttribute("off", "off");
|
||||
|
||||
model.addAttribute("isRaining", true);
|
||||
model.addAttribute("isSunny", true);
|
||||
model.addAttribute("isCold", false);
|
||||
model.addAttribute("isWarm", true);
|
||||
|
||||
return "booleans.html";
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.thymeleaf.customhtml;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Course {
|
||||
|
||||
private String name;
|
||||
private String description;
|
||||
private Date startDate;
|
||||
private Date endDate;
|
||||
private String teacher;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Date getStartDate() {
|
||||
return startDate;
|
||||
}
|
||||
|
||||
public void setStartDate(Date startDate) {
|
||||
this.startDate = startDate;
|
||||
}
|
||||
|
||||
public Date getEndDate() {
|
||||
return endDate;
|
||||
}
|
||||
|
||||
public void setEndDate(Date endDate) {
|
||||
this.endDate = endDate;
|
||||
}
|
||||
|
||||
public String getTeacher() {
|
||||
return teacher;
|
||||
}
|
||||
|
||||
public void setTeacher(String teacher) {
|
||||
this.teacher = teacher;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.thymeleaf.customhtml;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
/**
|
||||
* Handles requests for the student model.
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
public class CourseRegistrationController {
|
||||
|
||||
@RequestMapping(value = "/registerCourse", method = RequestMethod.POST)
|
||||
public String register(@ModelAttribute Course course, Model model) {
|
||||
model.addAttribute("successMessage", "You have successfully registered for course: " + course.getName() + ".");
|
||||
return "templates/courseRegistration.html";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/registerCourse", method = RequestMethod.GET)
|
||||
public String register(Model model) {
|
||||
model.addAttribute("course", new Course());
|
||||
return "templates/courseRegistration.html";
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -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;
|
||||
}
|
||||
}
|
||||
+27
@@ -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 + "]";
|
||||
}
|
||||
}
|
||||
+23
@@ -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";
|
||||
}
|
||||
}
|
||||
+64
@@ -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;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.thymeleaf.mvcdata;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
|
||||
|
||||
@Configuration
|
||||
public class BeanConfig {
|
||||
@Bean
|
||||
public EmailData emailData() {
|
||||
return new EmailData();
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.thymeleaf.mvcdata;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
|
||||
|
||||
@Controller
|
||||
public class EmailController {
|
||||
private EmailData emailData = new EmailData();
|
||||
private ServletContext servletContext;
|
||||
|
||||
public EmailController(ServletContext servletContext) {
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/email/modelattributes")
|
||||
public String emailModel(Model model) {
|
||||
model.addAttribute("emaildata", emailData);
|
||||
return "mvcdata/email-model-attributes";
|
||||
}
|
||||
|
||||
@ModelAttribute("emailModelAttribute")
|
||||
EmailData emailModelAttribute() {
|
||||
return emailData;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/email/requestparameters")
|
||||
public String emailRequestParameters(
|
||||
@RequestParam(value = "emailsubject") String emailSubject,
|
||||
@RequestParam(value = "emailcontent") String emailContent,
|
||||
@RequestParam(value = "emailaddress") String emailAddress1,
|
||||
@RequestParam(value = "emailaddress") String emailAddress2,
|
||||
@RequestParam(value = "emaillocale") String emailLocale) {
|
||||
return "mvcdata/email-request-parameters";
|
||||
}
|
||||
|
||||
@GetMapping("/email/sessionattributes")
|
||||
public String emailSessionAttributes(HttpSession httpSession) {
|
||||
httpSession.setAttribute("emaildata", emailData);
|
||||
return "mvcdata/email-session-attributes";
|
||||
}
|
||||
|
||||
@GetMapping("/email/servletcontext")
|
||||
public String emailServletContext() {
|
||||
servletContext.setAttribute("emailsubject", emailData.getEmailSubject());
|
||||
servletContext.setAttribute("emailcontent", emailData.getEmailBody());
|
||||
servletContext.setAttribute("emailaddress", emailData.getEmailAddress1());
|
||||
servletContext.setAttribute("emaillocale", emailData.getEmailLocale());
|
||||
return "mvcdata/email-servlet-context";
|
||||
}
|
||||
|
||||
@GetMapping("/email/beandata")
|
||||
public String emailBeanData() {
|
||||
return "mvcdata/email-bean-data";
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.thymeleaf.mvcdata.repository;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EmailData implements Serializable {
|
||||
private String emailSubject;
|
||||
private String emailBody;
|
||||
private String emailLocale;
|
||||
private String emailAddress1;
|
||||
private String emailAddress2;
|
||||
|
||||
public EmailData() {
|
||||
this.emailSubject = "You have received a new message";
|
||||
this.emailBody = "Good morning !";
|
||||
this.emailLocale = "en-US";
|
||||
this.emailAddress1 = "jhon.doe@example.com";
|
||||
this.emailAddress2 = "mark.jakob@example.com";
|
||||
}
|
||||
|
||||
public String getEmailSubject() {
|
||||
return this.emailSubject;
|
||||
}
|
||||
|
||||
public String getEmailBody() {
|
||||
return this.emailBody;
|
||||
}
|
||||
|
||||
public String getEmailLocale() {
|
||||
return this.emailLocale;
|
||||
}
|
||||
|
||||
public String getEmailAddress1() {
|
||||
return this.emailAddress1;
|
||||
}
|
||||
|
||||
public String getEmailAddress2() {
|
||||
return this.emailAddress2;
|
||||
}
|
||||
|
||||
public List<String> getEmailAddresses() {
|
||||
List<String> emailAddresses = new ArrayList<>();
|
||||
emailAddresses.add(getEmailAddress1());
|
||||
emailAddresses.add(getEmailAddress2());
|
||||
return emailAddresses;
|
||||
}
|
||||
}
|
||||
+28
@@ -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;
|
||||
}
|
||||
}
|
||||
+39
@@ -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;
|
||||
}
|
||||
}
|
||||
+62
@@ -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";
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.thymeleaf.requestparameters;
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
+13
@@ -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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user