Spring MVC for beginners

Model

It stores data and related logic in our application. it is responsible to transfer data between view and controller. it takes user inputs from view then sends it to the controller. controller takes those input and performs related logic and sends the required data to view using model.

View

It represents the UI of our application, it uses the data provided by the model and presents it to the user. ex.-page of a website.

Controller

A controller is the part of the application that interacts with the user inputs, it takes requests from the user, performs appropriate logic, creates an appropriate model, and passes it to view for presentation. ex.- when we click send, it sends the mail and gives us an appropriate response.

WHY?

When we are building our application we want our application to be easily testable and easy to update whenever need, so by using MVC we are separating three main components so they are less dependent on each other. so if we want to change the view we don’t have to change other things the same is with testing. nowadays it’s widely accepted by industry so lots of frameworks are available for this.

Example

let’s say I wanted to book a bus ticket and I went to a third party booking service called A. so when I will ask for the ticket A will go to a bus service with my booking information and then bus service will check in their data system that ticket is available or not, accordingly, A will get the response and with that response, A will come back and hand me that information in this case ticket(if booked).

Implementation

For this, I’m using spring boot but if you are using spring don’t worry, refer to the link given below and configure DispatcherController(its root of all controllers, all of the request comes to this controller first and then it decides which controller to call for which request), add tomcat japer and we are good to go. https://www.tutorialspoint.com/springmvc/springmvc_overview.htm

package com.example.demo;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {

@RequestMapping("/index")
public String getindex(Model m) {

m.addAttribute("message", "hi there");
return "index.jsp";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html><html>   <head>      <meta charset="UTF-8">      <title>Insert title here</title>   </head>   <body>      ${message}   </body></html>

Summery

Three components of MVC are model, view, and controller. the model basically transfers data between view and controller, the view presents that data, and controller works with user requests and updates model.

References

There is always more to learn.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store