Sample Spring MVC Form Validator

Technology Stack
Project Structure

Screen Shot 2020-02-23 at 7.36.46 PM

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
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency>
</dependencies>
 
 
Controller Layer
package com.spring.mvc.controller;
 
import javax.validation.Valid;
 
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 com.spring.mvc.form.StudentForm;
 
@Controller
public class ApplicationController {
 
@GetMapping(“/”)
public String landingPage(@ModelAttribute(“studentForm”) StudentForm studentForm) {
return “landing”;
}
 
@PostMapping(“/form”)
public String form(@Valid StudentForm studentForm, BindingResult bindingResult, Model model) {
 
if (bindingResult.hasErrors()) {
return landingPage(studentForm);
}
model.addAttribute(“studentForm”, studentForm);
return “welcome”;
}
 
}
 
Define Validation rules in StudentForm
 
package com.spring.mvc.form;
 
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
 
public class StudentForm {
 
@NotNull
@Size(min=5, max=30)
private String firstname;
 
@NotNull
@Size(min=5, max=30)
private String lastname;
 
@NotNull
@Size(min=5, max=30)
private String schoolname;
 
@NotNull
@Min(5)
@Max(20)
private Integer age;
 
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getSchoolname() {
return schoolname;
}
public void setSchoolname(String schoolname) {
this.schoolname = schoolname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
 
 
 
}
 
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

Screen Shot 2020-02-23 at 7.52.28 PM
landing page

Enter details and click submit. Validation rules will kick in.

Screen Shot 2020-02-23 at 7.52.36 PMScreen Shot 2020-02-23 at 7.53.14 PM

Enter Valid Data and click Submit -It will go to welcome page.

Screen Shot 2020-02-23 at 7.53.26 PM

Screen Shot 2020-02-23 at 7.53.33 PM
Welcome page

Click Go back Button – It will take you back to landing page

Screen Shot 2020-02-23 at 7.53.38 PM
landing page

Leave a Reply

Your email address will not be published. Required fields are marked *