About Spring MVC

Spring MVC নিয়ে কিছু কথা জেনে নিই

MVC মানে কী?

MVC মানে আমরা বুঝি Model-View-Controller, যা হল একটি প্যাটার্ন । এর মাধ্যমে আমরা সহজে আমদের অ্যাপ্লিকেশানের সকল স্টেজ ম্যাপ করতে পারি। (প্রথম ডায়াগ্রাম MVC এবং দ্বিতীয় ডায়াগ্রাম Spring MVC উপস্থাপন করছে)।

Image result for mvc architecture in design patterns

spring mvc

 

আমরা যদি MVC ডায়াগ্রামটা খেয়াল করি তাহলে বুঝতে পারব যে MVC প্যাটার্ন কীভাবে কাজ করে। আমরা একটু ভালভাবে বুঝার চেষ্টা করি।

Spring MVC is based on the MVC design pattern, it is a software architecture design pattern.  It provides solution to layer an application by separating three concerns business, presentation and control flow.

  • Model – Model represents an object or JAVA POJO carrying data. It can also have logic to update controller if its data changes.
  • View – View represents the visualization of the data that model contains.
  • Controller – Controller acts on both model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps view and model separate.

MVC Pattern UML Diagram

উপরের চিত্রর সাথে আমরা নিচের কোড মিলিয়ে স্টেপ অনুযায়ী অনুশীলন করিঃ

Step 1:  আমরা মডেল তৈরি করি – Create a Java Class named “Student.java” with following code:

public class Student {
private String rollNo;
private String name;


public String getRollNo() {
return rollNo;
}


public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}

public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}
}

Step 2 : Create View – StudentView.java

public class StudentView {
public void printStudentDetails(String studentName, String studentRollNo){
System.out.println("Student: ");
System.out.println("Name: " + studentName);
System.out.println("Roll No: " + studentRollNo);
}
}

Step 3 : Create Controller : StudentController.java

public class StudentController {
private Student model;
private StudentView view;


public StudentController(Student model, StudentView view){
this.model = model;
this.view = view;
}


public void setStudentName(String name){
model.setName(name);
}


public String getStudentName(){
return model.getName();
}


public void setStudentRollNo(String rollNo){
model.setRollNo(rollNo);
}


public String getStudentRollNo(){
return model.getRollNo();
}


public void updateView(){
view.printStudentDetails(model.getName(), model.getRollNo());
}
}

Step 4 : Use the StudentController methods to demonstrate MVC design pattern usage.

MVCPatternDemo.java

public class MVCPatternDemo {
public static void main(String[] args) {


//fetch student record based on his roll no from the database


Student model = retriveStudentFromDatabase();


//Create a view : to write student details on console


StudentView view = new StudentView();

StudentController controller = new StudentController(model, view);
controller.updateView();


//update model data


controller.setStudentName("Nishi");
controller.updateView();
}
private static Student retriveStudentFromDatabase(){
Student student = new Student();
student.setName("Sana");
student.setRollNo("10");
return student;
}
}

Let’s create a Spring MVC project with the code and show the output in the comments. If you have any problem, you may also let us know… Happy Learning codes of Spring…

Leave a Reply

Your email address will not be published. Required fields are marked *