Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 19 January 2013

MVC Pattern and Frameworks (model, view and controller)

In this blog I will give you a brief  introduction on MVC pattern .We will see what mvc is ? and what are the components of MVC Pattern. As we all knows that MVC stands for Model, View and Controller , the overall design and responsibility is divided in three streams so that each of them can concentrate on its own part.

What problem is solved by MVC ?
Lets take an example of ancient web application with no MVC and where the servlet have to do everything at its own .

servlet-web-application-architecture

Servlet Task 1 : The first thing the servlet has to do is to get the request paramaters to act on accordingly.
Servlet Task 2 : The second task is to get data from database according to user request. The servlet has to establish a connection to the DB and do sql queries to collect data from the DB.
Servlet Task 3 : The third task is to collect the data and format that into HTML , the servlet would have to add a lot of HTML tags and other things before sending back the response to the user.
Servlet Task 4 : The final task is to return formatted data back to user who has sent the request.

How MVC solved the problem ?

web-mvc-architecture

Task 1 (servlet) : Servlet gets the request parameters and passes them to the service method.
Task 2 (service bean) : Service bean gets the parameters from servlet, connect to DB and retrive data accordingly . The data is send back to servlet in form of a bean.
Task 3 (jsp) : Jsp gets the response data and handles HTML formatting and all the stuff .
Task 4 (jsp) : The JSP is send back to user , jsp does not send the formatted data back to servlet because it already has the data ready to send back to user.

Model view and Controller can be defined as follows :
Model : There are a number of definition for a model. Some people says that the service bean and method that collects the data from DB is called a Model, other say that the data itself that is being retrived from the DB is model. In fact the model is made up of business and data part of an application. So we can say that the process of getting data and the data itself forms the term model.
View : View is actually the part that renders the model in form of presentation in front of user. It is the representational part of an application , view is rendered in form of HTML to the browser.
Controller : The controller is what manages the data flow throughout the application. It choose the appropriate model and passes the requested parameters to is and let the model to retrieve the data from DB and than decides, what should be the view part of the request.

Advantages of MVC
1-     Controller and views are separated from model. So the controller does not care about how the data would be collected and how this would be represented. In the same way the view need to care about the representation only and the model needs to retrieve the data only.
2-     Views can be changed without being affecting the model and controlled part of the application.
3-     The same service bean can be used in an desktop application, so reusability is enhanced .

MVC Frameworks
MVC framework is the classes and libraries that helps us in building application based on MVC pattern. There are a number of MVC framework for web, desktop and mobile applications. Some of the Java web application frameworks are Struts, Struts2, SpringMVC, Wicket, JSF … and much more. The all are different in some aspects but the core concept of model view and controller is same.











Thanks for reading !
Being Java Guys Team



1 comment:

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft