How not losing your mind trying to understand MVC. Let me explain what it is? And how to implement it in a basic way.
What is MVC?
Is the solution to the problem of how to connect the client with the server, in this case in a Web application quickly, organized and simple. With Spring Boot Framework You can implement the model-view-controller best design pattern as MVC, where the layer called View(HTML or JSP files) that is normally the client in our Web application communicates with the business layer (Java files Called controllers) that would be the server, by means of models (Java files better known as Plain Old java Object). To understand how to communicate a Web application that implements the design pattern MVC. Take a look to the following image: The client does a request to the dispatcher of Servlets and this calls the URL mapping Manager, identifies to which controller and what method you want Access and return that information to the dispatcher to invoke that method.
In the statement of the method you can put your model (POJO) and if the attributes of the request are equal to the attributes of your POJO, the implementation of Spring Boot MVC Assign those values automatically, allowing you to use that information for your logic of business and Persistence.
Once finished the previous process you will want to show some kind of message or Web page, this is where the view comes in.
You must tell Spring Boot which is the next page and what information you want to send. Spring Boot will create a view (response) based on that information and return it to the customer.
Let’s get to work.
Step by step:
In the following video I’ll show you step by step how to create a WEB application, with MVC architecture, you can also find the code on GitHub to be able to match it with yours.
GitHub: GitHub repository
- Create Project:
- Make sure you use the Spring boot INITIALIZR
- Select the type of packaging war
- Select the WEB dependency
- Finish (If you need help creating project with SpringBoot review my tutorial Spring Boot-What is it? And how do you eat it?
- Settings to interpret JSP files:
- Create the folder structure src/main/webapp/WEB-INF/JSP
- Create the JSP files your project needs
- Adds these two attributes in the application. Properties File:
- Spring.Mvc.View.Prefix:/WEB-INF/jsp/
- Spring.Mvc.View.Suffix:.jsp
- Adds the following dependency to the application. Properties file
Ok, and your project is ready to run JSP files, proceed to create the access points or drivers:
- Creates a Java class and in the class statement adds the notation @Controller
- There are different ways to create methods depending on the purpose:
- To render a JSP, declare a method that returns a String (the name of the JSP)
- Add the notation @GetMapping and within your constructor add the path in the URL by any access to that JSP
- If you want more example of how to send and receive data to and from a JSP look at the video above.
Do not use more JSP and take a step forward and more productive creating code cleaner and understandable for others, using Thymeleaf.
Don’t forget to see the second part of this tutorial. Spring Boot + MVC Part 2 | Using Thymeleaf
Spring boot is very popular for creating pages and Web services, little by little will explain more thoroughly the unique features and capabilities of spring boot. I hope this post answer your questions and if you have any suggestions and/or contributions please leave it in the comments.
Thank you,
@Cruizg93
Spring Boot Introduction:
- Spring Boot – 01 – What is it? And how do you eat?
- Spring Boot + MVC Part 1 | Using JSP? Current
- Spring Boot + MVC Part 2 | Using Thymeleaf
- Spring Boot CRUD + MVC + JPA + H2 Part 1
- Spring Boot CRUD + MVC + JPA relations 1:1 and *:*