Hi, Hope you are doing well. This post is focused on Spring MVC framework.
Let start with introduction.
What is Spring MVC?
As the name suggests, it's a module of the Spring framework dealing with the Model-View-Controller, or MVC pattern.
It combines all the advantages of the MVC pattern with the convenience of Spring.
It implements all the basic features of a core spring framework like Inversion of Control, Dependency Injection.
Spring Framework + MVC
Spring MVC helps in building flexible and loosely coupled web applications. the model-view-controller design pattern helps in separating the business logic, presentation logic, and navigation logic.
models are responsible for encapsulating the application data.
views render a response to the user with the help of the model object.
controllers are responsible for receiving the request from the user and calling the back-end services.
How Spring MVC works?
When a request is sent to the spring mvc framework the following sequence of events happen.
the dispatcherservlet first receives the request.
the dispatcherservlet consults the handlermapping and invokes the controller associated with the request.
the controller processes the request by calling the appropriate service methods and returns a modelandview object to the dispatcherservlet . the modelandview object contains the model data and the view name.
the dispatcherservlet sends the view name to a viewresolver to find the actual view to invoke.
now, the dispatcherservlet will pass the model object to the view to render the result.
the view , with the help of the model data, will render the result back to the user.
Create First Project of Spring MVC
Go to file -> new -> dynamic web project to create a web project.
enter the project name and click the finish button.
right-click the project folder, and select spring tools -> add spring project nature to add spring capabilities to the web project. this feature will be available once you install the spring ide.
create a new package com.vaannila inside the src directory. the spring controller class extends org.springframework.web.servlet.mvc.abstractcontroller class. to create a new controller class, right-click the src directory and create a new java class, enter the controller class name and super class name, and then press the finish button.
copy the following code inside the helloworldcontroller class.
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.web.servlet.modelandview;
import org.springframework.web.servlet.mvc.abstractcontroller;
public class helloworldcontroller extends abstractcontroller {
private string message;
@override
protected modelandview handlerequestinternal(httpservletrequest request, httpservletresponse response) throws exception {
return new modelandview("welcomepage","welcomemessage", message);
}
public void setmessage(string message) {
this.message = message;
}
}
​
Spring MVC Dependencies
Our generated pom.xml file looks like below :
<?xml version="1.0" encoding="UTF-8"?><project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion>
<groupId>com.codersarts</groupId>
<artifactId>SpringMVCExample</artifactId>
<name>SpringMVCExample</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>4.0.0.RELEASE</org.springframework-version><org.aspectj-version>1.7.4</org.aspectj-version><org.slf4j-version>1.7.5</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
To run this example, you need to load:
Spring Core jar files
Spring Web jar files
JSP + JSTL jar files (If you are using any another view technology then load the corresponding jar files).
Advantages of Spring MVC Framework
Let's see some of the advantages of Spring MVC Framework:-
Separate roles - The Spring MVC separates each role, where the model object, controller, command object, view resolver, DispatcherServlet, validator, etc. can be fulfilled by a specialized object.
Light-weight - It uses light-weight servlet container to develop and deploy your application.
Powerful Configuration - It provides a robust configuration for both framework and application classes that includes easy referencing across contexts, such as from web controllers to business objects and validators.
Rapid development - The Spring MVC facilitates fast and parallel development.
Reusable business code - Instead of creating new objects, it allows us to use the existing business objects.
Easy to test - In Spring, generally we create JavaBeans classes that enable you to inject test data using the setter methods.
Flexible Mapping - It provides the specific annotations that easily redirect the page.
Some Common tasks in Spring MVC?
Jsp web applications
Web API's
Develop MVC applications
Employee management system using spring
How Codersarts can help you in Spring MVC?
Codersarts provide
Spring MVC assignment help
Development project help
Mentorship in Spring MVC
Error Resolving in Spring MVC
If you are looking for any kind of help in Spring MVC, Contact us
Comments