From fa880db84ec4507010a4d29b7cb0308f82553834 Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Sat, 13 Jun 2026 17:27:05 +0530 Subject: Added sample project 07-thymeleaf-common-theme --- .../main/java/com/example/spring/Application.java | 12 +++ .../java/com/example/spring/HomeController.java | 13 +++ .../example/spring/contact/ContactController.java | 34 ++++++++ .../com/example/spring/contact/ContactForm.java | 45 +++++++++++ .../src/main/resources/application.properties | 0 .../resources/config/application-dev.properties | 10 +++ .../src/main/resources/static/css/main.css | 92 ++++++++++++++++++++++ .../src/main/resources/templates/contact/form.html | 30 +++++++ .../main/resources/templates/contact/result.html | 14 ++++ .../src/main/resources/templates/index.html | 11 +++ .../src/main/resources/templates/layout/main.html | 31 ++++++++ 11 files changed, 292 insertions(+) create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/Application.java create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/HomeController.java create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/contact/ContactController.java create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/contact/ContactForm.java create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/resources/application.properties create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/resources/config/application-dev.properties create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/resources/static/css/main.css create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/contact/form.html create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/contact/result.html create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/index.html create mode 100644 spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/layout/main.html (limited to 'spring-boot/07-thymeleaf-common-theme/src/main') diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/Application.java b/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/Application.java new file mode 100644 index 0000000..4393f3f --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/Application.java @@ -0,0 +1,12 @@ +package com.example.spring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) throws Exception { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/HomeController.java b/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/HomeController.java new file mode 100644 index 0000000..d6ead56 --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/HomeController.java @@ -0,0 +1,13 @@ +package com.example.spring; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class HomeController { + + @RequestMapping("/") + public String home() { + return "index"; + } +} diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/contact/ContactController.java b/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/contact/ContactController.java new file mode 100644 index 0000000..20c482d --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/contact/ContactController.java @@ -0,0 +1,34 @@ +package com.example.spring.contact; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; + +import jakarta.validation.Valid; + +@Controller +public class ContactController { + + @GetMapping("/contact") + public String showForm(Model model) { + model.addAttribute("contactForm", new ContactForm()); + return "contact/form"; + } + + @PostMapping("/contact") + public String processSubmission(@Valid @ModelAttribute ContactForm contactForm, BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + return "contact/form"; + } + + // Do whatever needed with the submitted data here. + // For example, invoke a method in a service/componet bean to send an email to contact person. + + return "contact/result"; + } + +} diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/contact/ContactForm.java b/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/contact/ContactForm.java new file mode 100644 index 0000000..c641edf --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/java/com/example/spring/contact/ContactForm.java @@ -0,0 +1,45 @@ +package com.example.spring.contact; + +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; + +public class ContactForm { + + @Pattern(regexp = "^[ a-zA-Z]+$", message = "Name should only include English letters") + @NotBlank(message = "Name is required") + private String name; + + @Email(message = "Must be a valid email") + @NotBlank(message = "Email is required") + private String email; + + @Size(min = 10, message = "Message must be at least 10 characters long") + private String message; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + +} diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/resources/application.properties b/spring-boot/07-thymeleaf-common-theme/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/resources/config/application-dev.properties b/spring-boot/07-thymeleaf-common-theme/src/main/resources/config/application-dev.properties new file mode 100644 index 0000000..f0f380b --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/resources/config/application-dev.properties @@ -0,0 +1,10 @@ +# This file contains settings used during development. +# If using Maven to run, try the following command: +# mvn spring-boot:run -Dspring-boot.run.profiles=dev + +# Disable caching for static resources +spring.web.resources.cache.period=0 +spring.thymeleaf.cache=false + +# Optional: Tell DevTools to watch changes but never restart the backend server for CSS +spring.devtools.restart.exclude=static/**,public/**,templates/** diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/resources/static/css/main.css b/spring-boot/07-thymeleaf-common-theme/src/main/resources/static/css/main.css new file mode 100644 index 0000000..edd53ea --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/resources/static/css/main.css @@ -0,0 +1,92 @@ +body { + font-size: 1.125em; + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + line-height: 1.6em; + color: rgb(32, 34, 36);; + background-color: #FFF; + padding: 10px 5px; +} + +a { + color: #1475AC; + text-decoration: none; +} + +a:hover { + text-decoration: underline; +} + +ul li { + padding-bottom: 0.5em; +} + +header { + text-align: left; + font-size: 1.2em; +} + +header h1 { + margin-top: 0; + margin-bottom: 20px; +} + +header a { + color: #000000; + text-decoration: none; +} + +.main-nav { + margin-top: 0; + padding: 0; +} + +.main-nav ul { + margin: 0; + padding : 0; + list-style-type: none; + background-color: #F6F8F8; + border: 1px solid #eee; +} + +.main-nav ul li { + margin-left: 0; + display: inline; + padding: 0 8px; + border-left: 1px solid #F9F9FF; + border-right: 1px solid #F9F9FF; +} + +.main-nav ul li:hover { + background-color: white; + border-left: 1px solid #addfff; + border-right: 1px solid #addfff; +} + +.main-nav ul li a { + color: #555; + font-weight: bold; +} + +.main-nav ul li ai:hover { + color: #000; +} + +footer { + margin-top: 20px; + font-size: 85%; + border-top: 1px solid #CCC; +} + +.form-field-container { + margin-top: 8px; + margin-bottom: 8px; +} + +.form-field-container label { + display: inline-block; + width: 5em; +} + +.form-field-container .validation-error { + color: red; +} \ No newline at end of file diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/contact/form.html b/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/contact/form.html new file mode 100644 index 0000000..7debe1f --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/contact/form.html @@ -0,0 +1,30 @@ + + + + +
+
+
+ + +
+
+ +
+ + +
+
+ +
+ + +
+
+ + +
+
+ + + \ No newline at end of file diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/contact/result.html b/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/contact/result.html new file mode 100644 index 0000000..d1bda9a --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/contact/result.html @@ -0,0 +1,14 @@ + + + + +
+

We received your message.

+ +

+ + Try again +

+ + + \ No newline at end of file diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/index.html b/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/index.html new file mode 100644 index 0000000..b2f59f2 --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/index.html @@ -0,0 +1,11 @@ + + + + +
+

Hello! This is the home page content.

+

Use the navigation links to explore other pages.

+
+ + + \ No newline at end of file diff --git a/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/layout/main.html b/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/layout/main.html new file mode 100644 index 0000000..b91f6f5 --- /dev/null +++ b/spring-boot/07-thymeleaf-common-theme/src/main/resources/templates/layout/main.html @@ -0,0 +1,31 @@ + + + + + Base title not shown + + + + + +
+

ABC App

+
+ + + +

Page content area title

+ + + + + + + \ No newline at end of file -- cgit v1.2.3