Introduction to Spring MVC for Web development

Introduction

pring MVC is a framework used for the development of web applications using java ee technology. It comes under the spring project umbrella which, is a bunch of projects built around spring architecture principles. Spring follows the best coding practices and design patterns, which allow them to make their framework easy to use and learn. Spring contains a bunch of projects like Spring Security, Spring Boot, Spring Microservices, etc. Each project focuses on a different aspect of application development. Spring MVC is one such project which focuses on the web development aspect of an application. So what it is, we’ll see it in the following sections. But before that, there are some prerequisites you should know to learn spring. Which is Java off course, which I assume you know already. And other is servlet and JSP. If you wish, you can go to the official website of JSP and Servlet, of Java ee to learn about Servlet and JSP technology. Or you can have a look at my previous blog, which gives a brief about those technologies.

What is MVC?

MVC in Spring MVC stands for Model View Control. It is a design pattern Spring MVC follows and forces it on web developers to built web applications. Now we’ll see one by one what this Model, View, and Controller is.

  • Model : Model is simply data or objects that are passed to the Controller and view. In order them to communicate with each other. These Models can be bean classes, a simple java POJO’s, or data coming from a database doesn’t matter. Model is just a representation of data that can be understood by both the view and controller.
  • View : The view is a result that the user sees after the completion of his request. The view is also an interface user interacts with to use the web application. Generally, this view is web pages that give the user an interactive way to communicate with the application.
  • Controller : The controller is an section where we write our core business logic. The controller gets the data from the View, processes it, and generates the model. Then passes that model back to the View to give a response back to the user.

Spring MVC Workflow

Fig. Spring MVC request response Workflow.
  • The Spring MVC is created on top of a Simple Servlet and JSP technology. The Front Controller or Dispatcher servlet is an entry point to any spring MVC application. The front controller is a Java class who extends The HttpServlet class internally and acts as a doorway for the request coming in. and forwards those requests to the appropriate internal controller.
  • After The request is forwarded to the controller, it performs internal business logic on that request and generates the appropriate response to send back to the Front controller. This response can be a java bean or a Collection of beans. This response is encapsulated inside of the model to send it. Here the model is just a container for data.
  • After this model containing data is forwarded to the view resolver, which forwards this model to the appropriate view template so that it can generate an appropriate web page to send as a response to a client using the data generated by a controller.

Why use Spring MVC over other frameworks?

  • The Spring MVC framework is very flexible and easy to use. Which can be extended easily for enterprise-level applications
  • The Spring MVC framework belongs to the spring family of projects. Hence it works very well with other spring technologies and projects.
  • In the Spring MVC reusable business logic can be generated. Hence no code duplication is present.
  • Spring is annotation based. Hence it is easy to configure your existing code base by adding an appropriate annotation to them.

There are lots of other features spring MVC provides. You can check them out on their official site. Links are in the reference section.

Annotations In Spring MVC

In this section, we’ll talk about some of the most commonly used annotations for Spring MVC. Since this blog is just a tip of an iceberg. Please go to spring.io for more detailed documentation. There are plenty of other annotations, which are useful and require a lot of space in a blog. Hence we’ll be sticking with a few of them. Since they are most frequently used in web development.

@Controller : If you annotate a class with the @controller annotation, it indicates that the annotated class is a Controller, the controller is an extension of @component annotation from spring core. This annotation helps in the component scanning of spring. Component scanning means that if you annotate, the class with component annotation spring will register that component in its registry so that you can ask spring to give that to you whenever you need it.

package com.example;
import
org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExampleController {
// request mappings
}

@RequestMapping : This annotation is used on methods to map those methods to appropriate web requests. These methods are written inside of a controller class. Request mapping accepts lots of parameters. You can refer to this link to know about those parameters.

package com.example;
import
org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExampleController {
@RequestMapping
public String helloWorld() {
return "hello.jsp";
}
}

There are plenty of other annotations in Spring MVC. You can see their use cases and know more about them on spring’s official website here.

Conclusion

Spring MVC is a great tool for you if you want to build Enterprise-level Java applications. It is robust, versatile, and easy to use. Spring is a widely known name nowadays every, java developer knows to some extent about it. Using spring MVC helps you connect easily with other cool projects from springs. So that you can harness the power of spring. This blog is just an introduction to this big giant technology spring MVC. There is always more to know and learn about it. Since Spring MVC gets updated quickly, check the spring.io for more details and their other projects.

And thank you for reading.

References :

https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Controller.html

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/package-summary.html

Software Engineer