MVC
MVC
With MVC design pattern, we have following components on which our design
depends:
The idea behind MVC pattern is a very clear separation between domain
objects which represents real-world entities and the presentation layer we see
on the screen. Domain objects should be completely independent and should
work without a View layer as well.
In this lesson, we will try to establish a strong MVC example with a Spring
Boot application which uses Thymeleaf in the View layer.
Before we start building our application, it is always a good idea to structure it.
Based on the MVC pattern, we will have following layers in our application:
MVC Pattern Example
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-
8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
MVC Model
Let’s start defining the pieces and move with our model first. The first step for
a sample app is defining our Model class:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
private int age = 0;
This is a simple model class which can contain a Person’s information. Let’s
use the defined model in our Controller and Service.
MVC Controller
Now that we know what data to transmit in Model, let us define a Controller to
allow user to request this data:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class PersonController {
@Autowired
PersonService personService;
1. The base URL of the application presents a simple View we design next.
2. The /person URL of the application presents a simple View which tabulates
the person data.
We will see the Views in a minute. Before that, we need to provide the Service
and Repository layer as well.
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonRepository personRepository;
@Override
public Person createPerson(Person person) {
return personRepository.save(person);
}
@Override
public List<Person> getAllPersons() {
return personRepository.findAll();
}
}
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepository extends JpaRepository<Person,
Long> {
}
Excellent! Now that we’re done with the all the service and DB access logic,
we can finally try our View.
MVC View
Let’s finally put our View in place. We first define our greetings.html page
in src/main/resources/templates directory.
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<title>Spring Boot MVC</title>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/>
</head>
<body>
<h1>Add new person</h1>
<form action="#" th:action="@{/person}" th:object="${person}"
method="post">
<p>Name: <input type="text" th:field="*{name}"/></p>
<p>Age: <input type="number" th:field="*{age}"/></p>
<p><input type="submit" value="Submit"/> <input
type="reset" value="Reset"/></p>
</form>
</body>
</html>
Next, we define our result.html page as well:
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<title>Spring Boot MVC</title>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/>
</head>
<body>
<div th:if="${not #lists.isEmpty(people)}">
<h2>Person List</h2>
<table class="glyphicon glyphicon-calendar">
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="person : ${people}">
<td th:text="${person.id}"></td>
<td th:text="${person.name}"></td>
<td th:text="${person.Age}"></td>
</tr>
</table>
</div>
</body>
</html>
There are many advantages for using MVC pattern. Let’s state some of them
here: