summaryrefslogtreecommitdiff
path: root/spring-boot/06-form-handling-with-thymeleaf
diff options
context:
space:
mode:
Diffstat (limited to 'spring-boot/06-form-handling-with-thymeleaf')
-rw-r--r--spring-boot/06-form-handling-with-thymeleaf/README12
-rw-r--r--spring-boot/06-form-handling-with-thymeleaf/pom.xml8
-rw-r--r--spring-boot/06-form-handling-with-thymeleaf/src/main/resources/config/application-dev.properties10
-rw-r--r--spring-boot/06-form-handling-with-thymeleaf/src/main/resources/static/css/main.css13
-rw-r--r--spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/form.html13
-rw-r--r--spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/contact/result.html1
-rw-r--r--spring-boot/06-form-handling-with-thymeleaf/src/main/resources/templates/index.html1
7 files changed, 52 insertions, 6 deletions
diff --git a/spring-boot/06-form-handling-with-thymeleaf/README b/spring-boot/06-form-handling-with-thymeleaf/README
index c286447..1d6a8a0 100644
--- a/spring-boot/06-form-handling-with-thymeleaf/README
+++ b/spring-boot/06-form-handling-with-thymeleaf/README
@@ -1,3 +1,15 @@
+This sample project uses Spring Boot DevTools to hot swap (or hot reload) the application when artifacts are updated.
+
+To run with support for hot swap properly, use the dev profile as follows:
+
+ mvn spring-boot:run -Dspring-boot.run.profiles=dev
+
+Check the dev profile configuration in application-dev.properties file.
+
+You may also enable an LiveReload extension in your browser for more convenience.
+
+####
+
This sample project is based on the excellent article found at below given URL:
https://medium.com/@AlexanderObregon/handling-form-submissions-with-spring-boot-using-thymeleaf-30dd41dda76d
diff --git a/spring-boot/06-form-handling-with-thymeleaf/pom.xml b/spring-boot/06-form-handling-with-thymeleaf/pom.xml
index c486563..6708292 100644
--- a/spring-boot/06-form-handling-with-thymeleaf/pom.xml
+++ b/spring-boot/06-form-handling-with-thymeleaf/pom.xml
@@ -23,6 +23,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
+
+ <!--This is to automatically reload web pages during development -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-devtools</artifactId>
+ <optional>true</optional>
+ </dependency>
+
</dependencies>
</project> \ No newline at end of file
diff --git a/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/config/application-dev.properties b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/config/application-dev.properties
new file mode 100644
index 0000000..f0f380b
--- /dev/null
+++ b/spring-boot/06-form-handling-with-thymeleaf/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/06-form-handling-with-thymeleaf/src/main/resources/static/css/main.css b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/static/css/main.css
new file mode 100644
index 0000000..8fcc7b3
--- /dev/null
+++ b/spring-boot/06-form-handling-with-thymeleaf/src/main/resources/static/css/main.css
@@ -0,0 +1,13 @@
+.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/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
index 0229e0d..52f51f2 100644
--- 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
@@ -4,28 +4,29 @@
<head>
<title>Contact Us</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <link rel="stylesheet" th:href="@{/css/main.css}" />
</head>
<body>
<h1>Contact Us</h1>
<form th:action="@{/contact}" th:object="${contactForm}" method="post">
- <div>
+ <div class="form-field-container">
<label>Name</label>
<input type="text" th:field="*{name}" placeholder="Your name" />
- <div th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
+ <div class="validation-error" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
</div>
- <div>
+ <div class="form-field-container">
<label>Email</label>
<input type="email" th:field="*{email}" placeholder="Your email" />
- <div th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
+ <div class="validation-error" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
</div>
- <div>
+ <div class="form-field-container">
<label>Message</label>
<textarea th:field="*{message}" placeholder="Your message"></textarea>
- <div th:if="${#fields.hasErrors('message')}" th:errors="*{message}"></div>
+ <div class="validation-error" th:if="${#fields.hasErrors('message')}" th:errors="*{message}"></div>
</div>
<button type="submit">Send</button>
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
index 1bb4b45..607a9eb 100644
--- 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
@@ -3,6 +3,7 @@
<head>
<title>Contact Us</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <link rel="stylesheet" th:href="@{/css/main.css}" />
</head>
<body>
<h1>Contact Us</h1>
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
index 48e251d..9259f47 100644
--- 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
@@ -4,6 +4,7 @@
<head>
<title>Form handling example with Thymeleaf</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <link rel="stylesheet" th:href="@{/css/main.css}" />
</head>
<body>