diff options
Diffstat (limited to 'spring-boot/06-form-handling-with-thymeleaf/src')
8 files changed, 176 insertions, 0 deletions
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> + |
