Are you a Technical Lead or Technical Project manager? Most likely you will work with JIRA to implement Spring MVC Application based solution. Gain more expertise on JIRA by learning top 10 JIRA features.
What is a MVC Framework?
In MVC Framework, MVC stands for Model View and Controller. This is a type of design pattern used in almost all Java web application developed today. It really help to categorize the different layers of Java web application.
So let us understand what does Model, View and Controller means in MVC framework
Model – This layer help to hold the data which can be shared between the next two layers. So we can call this as data carrier which carries data from one layer to another.
View – This layer is solely responsible for the managing the view part for a Java web application. As the name suggest, this layer only take care of views. In this layer we can use JSP, HTML, CSS, XHTML etc. Also, there are many template engine available which helps us in designing the Java web applications like velocity, freemaker, thyemleaf etc.
Controller – This layer is responsible to take the data from view layer process and send back as response. This is the reason we call this as controller. The controller manages the flow of data between view and model layers.
This is an Architectural Design pattern which is used for enterprise solution. You may also like to learn about design patterns to re-use code level implementations. Learn Top 10 ways to implement singleton design patterns.
What is a Spring Framework?
Spring is a power full framework which now a days is used globally for developing the enterprise based applications. It comes with different flavors like IOC, Web, Jpa, JDBC, Cloud etc. The modern world need such solutions to develop Java web application.
In this tutorials we are going to explore different parts of creating a Spring boot Web MVC application which is going to have a RESTful web service and respond to your request.
To implement spring boot Web MVC application we need following software
- IDE- Spring tool Suite (STS 3.9.0)
- JDK – 1.8
- Maven – 3.5.0
Now let’s learn how to build spring boot Web MVC application step by step.
Step 1: How to Create Workspace in Spring MVC Eclipse?
- Open the Spring IDE in Eclipse.
- You will see following Eclipse Launcher screen.
- Enter the desired Spring MVC Project workspace name.
- Click on Launch to create your work space for the Spring MVC
Step 2: How to create Spring MVC project in STS?
- Download apache-maven-3.5.0-bin.zip.
- Extract this zip file in a folder.
- Locate settings.xml file in “apache-maven-3.5.0/Config” folder
- Refer this file location in Preferences à Maven à User settings. Please refer to the image below.
- Once Spring MVC STS Setup is done, it reads your settings.xml and download the dependencies at your mentioned folder.
Step 3: How to Create Spring Web Project?
To create new Spring Web Project:
- Click on File -> New -> Spring Strater Project
- In the new spring starter project, give the desired project name to the new project.
- Provide the group id for the project
- Provide Artifact id for the project
- Write the project description for the project
- Provide the package name for the project
Packaging – By default in Eclipse, the project packaging is selected as “jar”.
For a web application, we need to change this to “war”.
Click on next to select the dependency of the project.
For a web application, choose the default dependency and again click on next. You will see the following image.
Click finish to create the project from this wizard.
Once project setup is done we will get a project structure like below.
The project, once created, will add maven dependencies by its own. It will also generate a few classes with inbuilt framework. Now you can run this project as Spring MVC application.
SpringTutorialsWebmvcApplication: This class is holding the annotation @SpringBootApplicaiton. This annotation is used by spring container to know that this is a spring boot application and run this class as main class to load its dependencies and classes to server.
@ComponentScan annotation helps to identify all the spring annotated classes loaded at server startup. A single web application can have multiple packages. The Scan annotation helps to identify the annotated classes to spring container. Hence, all the annotated classes gets loaded to the spring container.
ServletInitializer: This class is used to register your Spring MVC web application in running web or application container.
Step 4: How to customize your Spring MVC application?
Now let’s add a static page for the Spring MVC application inside static folder with name index.html. Please refer to the image below.
After adding index.html, right click on the spring-tutorials-webmvc project and run as Spring Boot App.
Your application will show you the default landing page like below.
The spring boot application is embedded in the tomcat server. Hence, you may execute application directly by clicking right mouse button on the spring-tutorials-webmvc and select “Run As Spring boot app”.
You can deploy this application on any other server. Once the spring mvc application is live, you can access the Spring MVC application at http://localhost:8080.
Your application will show you the default landing page. You can customize the landing page using few tweaks. Let’s try a few things. A server can handle multiple applications at any given time provided we define a project context path with specific port number. We can modify this from application.properties. Please refer image below.
Apply above configuration and restart application.
Step 5: How to add a Controller?
Add a controller for handling the browser request by adding highlighted section.
In the above example, let’s create a new controller for handling the greetings request.
@RequestMapping – This annotation add request mapping “/rest” to all the request method in this controller class.
@RestController – This annotation make this class work as Rest Controller.
@GetMapping – This annotation make this method behave as get request. The is having url pattern as “/greetings”.
@RequestParam – This annotation make the request parameter of url available to this method. We also assigned default value for the parameter as “World”. If there will be no parameter this default value will be assigned.
After adding these changes if we run the application and try to access url you will see a page with “Hello World” as shown below.
After passing parameter.
This is the final modification for the Spring MVC application.
I am sure this article will help you boost your Spring boot MVC web application knowledge. And if it did, please share a comment and valuable feedback for me.
Folks, what you would like to read next? Let me know.
Thanks for reading this post on Spring MVC Application. Please share this post with your friends and help them.
See you soon in the next article.