What is MVC (Model View Controller)
MVC (Model View Controller) is a software design pattern commonly used for developing user interfaces. It divides the related program logic into three interconnected elements:
- Model: Represents the data-related logic that the user works with. It can include data transferred between the View and Controller components or any other business logic-related data. The Model interacts with the database and provides the required data back to the Controller.
- View: Handles all the UI logic of the application. It generates the user interface for the user. Views are created based on data collected by the Model component, but this data is obtained through the Controller. The View only interacts with the Controller.
- Controller: Acts as an intermediary between the views and the model. It processes all the business logic and incoming requests, manipulates data using the Model component, and interacts with the View to render the final output. The Controller doesn’t handle data logic directly; it instructs the Model on what to do.
MVC provides several benefits:
- Clear separation of business logic, UI logic, and input logic
- Full control over HTML and URLs, making it easy to design web application architecture
- Powerful URL-mapping component for comprehensible and searchable URLs
- Support for Test Driven Development (TDD)
Originally used for desktop graphical user interfaces (GUIs), MVC is now widely used in web development and mobile app design. It helps create scalable and extensible projects by isolating different aspects of an application.
In essence, the MVC pattern decouples these major components allowing for efficient code reuse and parallel development1. It’s widely used in web applications but also applicable in other programming domains2.
Here is the Controller page (HomeController)
HomeController.java
Spring MVC
Spring MVC
package com.siteinvokers.SpringMVCBoot;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
// Method will accept request for /
@RequestMapping("/")
public ModelAndView home(){
ModelAndView mv = new ModelAndView("index") ;
return mv ;
}
@RequestMapping("add")
// code for request mapping is given below
}
Here is the Views for index page and result page
Index page
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hi There,
<BR>
Welcome to siteinvokers
<br>
<form method="get" action="add">
Enter 1st Number : <input type="text" name="num1" /><br>
Enter 2nd Number : <input type="text" name="num2" /><br>
<input type="submit"/>
</form>
</body>
</html>
Below is View page
view.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Result is : ${sum}
</body>
</html>
Ways for Setting variables for View
Ways for Setting variables for View using Session Object
We can set the variables for the view using the session object. Below is the controller code for setting value using session.
public String add( @RequestParam("num1") int i, @RequestParam("num2") int j,
HttpSession session ){
// int i = Integer.parseInt(req.getParameter("num1"));
// int j = Integer.parseInt(req.getParameter("num2"));
int sum = i+j ;
// passing value by session
// HttpSession session = req.getSession();
session.setAttribute("sum", sum) ;
return "result.jsp" ;
}
Ways for Setting variables for View using Model And View
We can set the variables for the view using the session object. Below is the controller code for setting value using ModelAndView.
@RequestMapping("add")
public ModelAndView add( @RequestParam("num1") int i, @RequestParam("num2") int j ){
int sum = i+j ;
// adding data using model and view
ModelAndView mv = new ModelAndView() ;
mv.setViewName("result");
mv.addObject("sum", sum) ;
return mv ;
}
Ways for Setting variables for View using Model Object in Parameters
We can set the variables for the view using the session object. Below is the controller code for setting value using model.
@RequestMapping("add")
public String add( @RequestParam("num1") int i, @RequestParam("num2") int j, Model m ){
int sum = i+j ;
// using model
m.addAttribute("sum", sum) ;
return "result" ;
}