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.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.siteinvokers.SpringMVCBoot.model.Programmer;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
// Method will accept request for /
@RequestMapping("/")
public ModelAndView home(){
ModelAndView mv = new ModelAndView("index") ;
return mv ;
}
@RequestMapping("add")
public String add( @RequestParam("num1") int i, @RequestParam("num2") int j, Model m ){
int sum = i+j ;
// adding data using model and view
// ModelAndView mv = new ModelAndView() ;
// mv.setViewName("result");
// mv.addObject("sum", sum) ;
// return mv ;
// using model
m.addAttribute("sum", sum) ;
return "result" ;
}
@RequestMapping("addProgrammer" )
// code for model will go here
}
Here is the model page (Programmer)
Spring MVC
Spring MVC
package com.siteinvokers.SpringMVCBoot.model;
public class Programmer {
private int pid;
private String pname ;
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
@Override
public String toString() {
return "Programmer [pid=" + pid + ", pname=" + pname + "]";
}
}
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>
<h2> For adding of two numbers </h2>
<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>
<h2> For adding user </h2>
<form method="get" action="addProgrammer">
Enter your id : <input type="text" name="pid" /><br>
Enter your name : <input type="text" name="pname" /><br>
<input type="submit"/>
</form>
</body>
</html>
Below is View page for programmer
programmer.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 : ${programmer}
</body>
</html>
Ways for Setting variables for View Using Model Attribute
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.
public String addProgrammer(@RequestParam("pid") int pid, @RequestParam("pname") String pname , Model m) {
Programmer p = new Programmer();
p.setPid(pid);
p.setPname(pname);
m.addAttribute("programmer" , p) ;
return "programmer";
}
Ways for Setting variables for View using ModelAttribute Annotation
We can set the variables for the view using the session object. Below is the controller code for setting value using model attribute annotation.
@RequestMapping("addProgrammer" )
public String addProgrammer( @ModelAttribute Programmer p , Model m) {
m.addAttribute("programmer" , p) ;
return "programmer";
}
Ways for Setting variables for View using ModelAttribute Annotation
We can set the variables for the view using the session object. Below is the controller code for setting value using model attribute annotation and removing the model object.
public String addProgrammer( @ModelAttribute("p1") Programmer p ) {
return "programmer";
}
programmer.jsp file
<%@ 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 : ${p1}
</body>
</html>
Ways for Setting variables for View using ModelAttribute Annotation
We can set the variables for the view using the session object. Below is the controller code for setting value using model attribute annotation.
@RequestMapping("addProgrammer" )
public String addProgrammer( @ModelAttribute Programmer p , Model m) {
m.addAttribute("programmer" , p) ;
return "programmer";
}