Technology Stack
- Spring Boot
- Spring MVC
- Thymeleaf
Project Structure
pom.xml
Add the below in pom.xml
- spring-boot-starter-parent – add this as parent
Add the below spring boot starters
- spring-boot-starter-web – needed for spring mvc
- spring-boot-starter-thymeleaf – needed for thymeleaf
Controller Layer
Define Validation rules in StudentForm
Application class annotated with @SpringBootApplication
package com.spring.mvc.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = “com.spring.mvc.controller”)
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
View Layer
Thymeleaf is a default template used by Spring Boot.
By default, Spring Boot looks for the templates in src/main/resources/templates location.
Add the html files inside src/main/resources/templates
landing.html
<html xmlns:th=″http://www.thymeleaf.org″>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Application</title>
<link rel=”stylesheet” href=″https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css″>
</head>
<body style=”width:50%;margin: 20px;padding: 20px;”>
<h3>Spring MVC Form Validator Application using Spring Boot and Thymeleaf</h3>
<div style=”margin: 20px;border: 1px solid blue;padding: 20px;”>
<div class=”form” style=”margin: 20px;padding: 20px;”>
<h4>Form</h4>
<form action=”#” th:action=”@{/form}” th:object=”${studentForm}” method=”post”>
<table class=”table table-bordered”>
<tr>
<td>First Name:</td>
<td><input type=”text” th:field=”*{firstname}” /></td>
<td th:if=”${#fields.hasErrors(‘firstname’)}” th:errors=”*{firstname}”>First Name Error</td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type=”text” th:field=”*{lastname}” /></td>
<td th:if=”${#fields.hasErrors(‘lastname’)}” th:errors=”*{lastname}”>Last Name Error</td>
</tr>
<tr>
<td>Age:</td>
<td><input type=”text” th:field=”*{age}” /></td>
<td th:if=”${#fields.hasErrors(‘age’)}” th:errors=”*{age}”>Age Error</td>
</tr>
<tr>
<td>School Name:</td>
<td><input type=”text” th:field=”*{schoolname}” /></td>
<td th:if=”${#fields.hasErrors(‘schoolname’)}” th:errors=”*{schoolname}”>School Name Error</td>
</tr>
</table>
<input type=”submit” value=”Submit”>
</form>
</div>
</div>
</body>
</html>
welcome.html
<html xmlns:th=″http://www.thymeleaf.org″>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Application</title>
<link rel=”stylesheet” href=″https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css″>
</head>
<body style=”width:50%;”>
<div style=”margin: 20px;padding: 20px;”>
<h3>Spring MVC Form Validator Application using Spring Boot and Thymeleaf</h3>
<div class=”form” style=”margin: 20px;border: 1px solid blue;padding: 20px;”>
<h4>Your Form Details are mentioned below:</h4>
<table class=”table table-bordered” style=”padding: 20px;”>
<tr>
<td>First Name</td>
<td>[[${studentForm.firstname}]]</td>
</tr>
<tr>
<td>Last Name</td>
<td>[[${studentForm.lastname}]]</td>
</tr>
<tr>
<td>Age</td>
<td>[[${studentForm.age}]]</td>
</tr>
<tr>
<td>School Name</td>
<td>[[${studentForm.schoolname}]]</td>
</tr>
</table>
<div class=”form”>
<form action=”/” method=”get”>
<table>
<tr>
<td><input type=”submit” value=”Go back”></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
Running Application
Execute the main method of Application.class and then hit http://localhost:8080
Enter details and click submit. Validation rules will kick in.
Enter Valid Data and click Submit -It will go to welcome page.
Click Go back Button – It will take you back to landing page