diff options
| author | Kamal Wickramanayake <kamal@inbox.lk> | 2026-06-06 20:02:35 +0530 |
|---|---|---|
| committer | Kamal Wickramanayake <kamal@inbox.lk> | 2026-06-06 20:02:35 +0530 |
| commit | 9b3396e2b5cb3660a94fb4a4c0dfb873b46ce224 (patch) | |
| tree | ff25c9c4583cd52cd30257be092f99892829b46e /spring-boot | |
| parent | 6657eb579ce0413689f2bfb0c2f912a930b3f179 (diff) | |
Added a Spring Boot + Thymeleaf based form validation sample application code
Diffstat (limited to 'spring-boot')
10 files changed, 206 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>com.example.spring.boot</groupId> + <artifactId>base-config</artifactId> + <version>0.0.1-SNAPSHOT</version> + <relativePath>../00-config</relativePath> + </parent> + + <artifactId>form-handling-with-thymeleaf</artifactId> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-thymeleaf</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-validation</artifactId> + </dependency> + </dependencies> + +</project>
\ 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 --- /dev/null +++ b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/application.properties 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 @@ +<!DOCTYPE HTML> +<html xmlns:th="http://www.thymeleaf.org"> + +<head> + <title>Contact Us</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +</head> + +<body> + <h1>Contact Us</h1> + + <form th:action="@{/contact}" th:object="${contactForm}" method="post"> + <div> + <label>Name</label> + <input type="text" th:field="*{name}" placeholder="Your name" /> + <div th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div> + </div> + + <div> + <label>Email</label> + <input type="email" th:field="*{email}" placeholder="Your email" /> + <div th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div> + </div> + + <div> + <label>Message</label> + <textarea th:field="*{message}" placeholder="Your message"></textarea> + <div th:if="${#fields.hasErrors('message')}" th:errors="*{message}"></div> + </div> + + <button type="submit">Send</button> + </form> + +</body> + +</html>
\ 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 @@ +<!DOCTYPE HTML> +<html xmlns:th="http://www.thymeleaf.org"> +<head> + <title>Contact Us</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +</head> +<body> + <h1>Contact Us</h1> + + <p>We received your message.</p> + + <p th:text="'Thank you ' + ${contactForm.name} + '.'" /> + + <a href="/contact">Try again</a> +</body> +</html>
\ 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 @@ +<!DOCTYPE HTML> +<html xmlns:th="http://www.thymeleaf.org"> + +<head> + <title>Form handling example with Thymeleaf</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +</head> + +<body> + +<h1>Form handling example with Thymeleaf</h1> + +<a href="/contact">Contact Us</a> + +</body> + +</html> + |
