From 9b3396e2b5cb3660a94fb4a4c0dfb873b46ce224 Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Sat, 6 Jun 2026 20:02:35 +0530 Subject: Added a Spring Boot + Thymeleaf based form validation sample application code --- .../06-form-handling-with-thymeleaf/.gitignore | 2 + .../06-form-handling-with-thymeleaf/pom.xml | 28 ++++++++++++++ .../main/java/com/example/spring/Application.java | 12 ++++++ .../java/com/example/spring/HomeController.java | 13 +++++++ .../example/spring/contact/ContactController.java | 36 +++++++++++++++++ .../com/example/spring/contact/ContactForm.java | 45 ++++++++++++++++++++++ .../src/main/resources/application.properties | 0 .../src/main/resources/templates/contact/form.html | 36 +++++++++++++++++ .../main/resources/templates/contact/result.html | 16 ++++++++ .../src/main/resources/templates/index.html | 18 +++++++++ 10 files changed, 206 insertions(+) create mode 100644 spring-boot/06-form-handling-with-thymeleaf/.gitignore create mode 100644 spring-boot/06-form-handling-with-thymeleaf/pom.xml create mode 100644 spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/Application.java create mode 100644 spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/HomeController.java create mode 100644 spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/contact/ContactController.java create mode 100644 spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/contact/ContactForm.java create mode 100644 spring-boot/06-form-handling-with-thymeleaf/src/main/resources/application.properties create mode 100644 spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/form.html create mode 100644 spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/result.html create mode 100644 spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/index.html (limited to 'spring-boot') diff --git a/spring-boot/06-form-handling-with-thymeleaf/.gitignore b/spring-boot/06-form-handling-with-thymeleaf/.gitignore new file mode 100644 index 0000000..3df278e --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/.gitignore @@ -0,0 +1,2 @@ +target +.vscode diff --git a/spring-boot/06-form-handling-with-thymeleaf/pom.xml b/spring-boot/06-form-handling-with-thymeleaf/pom.xml new file mode 100644 index 0000000..c486563 --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + + com.example.spring.boot + base-config + 0.0.1-SNAPSHOT + ../00-config + + + form-handling-with-thymeleaf + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-validation + + + + \ No newline at end of file diff --git a/spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/Application.java b/spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/Application.java new file mode 100644 index 0000000..4393f3f --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/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/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/HomeController.java b/spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/HomeController.java new file mode 100644 index 0000000..d6ead56 --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/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/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/contact/ContactController.java b/spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/contact/ContactController.java new file mode 100644 index 0000000..1a61a39 --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/contact/ContactController.java @@ -0,0 +1,36 @@ +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. + + model.addAttribute("contactForm", contactForm); + + return "contact/result"; + } + +} diff --git a/spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/contact/ContactForm.java b/spring-boot/06-form-handling-with-thymeleaf/src/main/java/com/example/spring/contact/ContactForm.java new file mode 100644 index 0000000..c641edf --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/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/06-form-handling-with-thymeleaf/src/main/resources/application.properties b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/form.html b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/form.html new file mode 100644 index 0000000..0229e0d --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/form.html @@ -0,0 +1,36 @@ + + + + + Contact Us + + + + +

Contact Us

+ +
+
+ + +
+
+ +
+ + +
+
+ +
+ + +
+
+ + +
+ + + + \ No newline at end of file diff --git a/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/result.html b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/result.html new file mode 100644 index 0000000..1bb4b45 --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/result.html @@ -0,0 +1,16 @@ + + + + Contact Us + + + +

Contact Us

+ +

We received your message.

+ +

+ + Try again + + \ No newline at end of file diff --git a/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/index.html b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/index.html new file mode 100644 index 0000000..48e251d --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/index.html @@ -0,0 +1,18 @@ + + + + + Form handling example with Thymeleaf + + + + + +

Form handling example with Thymeleaf

+ +Contact Us + + + + + -- cgit v1.2.3