Spring Model View Architecture. Third party image.
Hi friends,Topic for today is the Spring 3.0 MVC !!
The best way to learn something is to associate it with what you like the most!
My most favourite dish is Chilli Parotta! I believe that there is no better example than that which can be used here to learn Spring 3.0 MVC !!! Unfortunately, it may not be your favourite dish, but you can imagine your own dish as you read the lines.
Before tasting the spiciness of this dish, let's learn some technical jargons and let's make sure that those terms and definitions are burnt and roasted on ours brains very well.
Purpose of Spring MVC:
Why applications are rewritten using frameworks?
* To isolate different elements of a software.
* Facilitates loose-coupling and creates a more of configuration type environment.
* To support more functionalities like Internationalization and Localization.
What is MVC mean?
MVC stands for Model-View-Controller.
* Model in 'MVC' represents data and business logic of the application or raw data.
* View represents a JSP or JSF which processes & displays raw data in a user-friendly format.
* Controller receives web request performs little or no processing and returns a ModelAndView object.
Note: It is to be noted that Controller and View are the inputs and outputs of the MVC architecture.
Working of Spring MVC:
* Whenever a user clicks a link and submits a form through the web browser, a request is created and it carries information like URL and even other data(form data, application data etc etc). The first stop of the request is the Dispatcher Servlet, which is like a front-end controller. Dispatcher servlet's job is to send request to Spring MVC.
* A Controller is a Spring component that processes the request forwarded by Dispatcher servlet. An application may have several controllers. So a dispatcher servlet must consult one or more handler mapper to figure out which controller is the suitable. Handler mapping would pay attention to the URL carried by the request when makings its decision. Once controller has been chosen, dispatcher servlet drops the request at the controller's place and waits till controller processes it. Controller returns a Model and View object to the dispatcher servlet.
Note: ModelAndView contains both model data and a small hint to what view should be used to render the display. So it carries only a 'logical name' of view.
* The dispatcher servlet would now ask a view resolver to help find the actual JSP so that properly formatted data is displayed to the user.
* The model data is delivered to the view and view does the remainder of the task and the job is over!
Now let's apply the same concept to prepare a Chilli Parotta or Chilli Paratha.
Imagine as follows:
You have just walked into a local restaurant in your city. A waiter or a waitress(if you are lucky) greets you with a warm smile. He is like the Dispatcher Servlet. He receives your order and passes it to the chef who is like the front-end Controller. He prepares your beloved dish and returns back the raw dish(ModelAndView) to the waiter or dispatcher servlet. Now the waiter would proceed to another chef who is like a view resolver who adds some decorations, salt, spice along with some chilli sauce and onion raitha to make the dish appear more presentable, delicious and tasty(in our context, proper formatted output for the JSP).
Hope the dish was tasty!
Advantages of Spring Framework:
* Easier to maintain code as it is more of a configuration type. A properly designed controller class will have less processing. All major processes would be handled by Service class or Data Access Object classes.
* Easier to make future enhancements as there is isolation of different elements involved in overall process. Making future enhancements if the business logic changes would be easy.
So if the waiter is not doing his service properly he can be fired and easily replaced by a better person who does good job. A better Chef could be hired and just incase if you decide to have Chilli Paratha in Madurai style than Chennai style, you need a separate view altogether as Madurai's Chilli Paratha is entirely different.
That's it for today guys, looks like everyone is laughing... Learning could be fun too...
Till we meet again next time,
Cheers from Object-ville,
Regards,
Krishna Kumar.S
New Number 1.6, Old Number 1.5,
6th Generics Lane,
Objectville.

very spicy :)
ReplyDelete@Ramanan: Thanks for visiting our restaurant.
ReplyDelete