Java 101: MVC model2
So what exactly is MVC? In short, it stands for Model-View-Controller and it is an implementation of the layered architecture I was talking about in my first Java 101 entry.
Overview
Here is a very quick rundown on what each term in the MVC pattern means:
- Model – this represents the business rules that you are trying to code. Another way of looking at it is that the Model is what does all the work. It is what performs any and all transformations on the data that your application is required to handle.
- View – the view represents what the user sees and it is responsible for the collection and display of data for and from the model.
- Controller – the controller is what ties everything together, it is the link between the view and the model.
One way of looking at MVC is that the user views a web page which displays and/or collects data. When the user submits this page, it is submitted to the controller which knows what to do with the data collected by that page. The controller does not know how to transform the data, but it knows which part of the model contains this knowledge and passes the data onto the model. The model then transforms the data. The controller, based on whether the transformation was a success or not, determines which part of the view to call and returns to that view a piece of the model in order for the view to display more data to the user. (Did you follow all of that?).
An Example
Here is a quick example: A user login page.
- The user sees the login page with a form that contains a field for the username and the password.
- The user fills in this data and submits it to the User controller with an action of login.
- The User controller knows to pass this information on to the loginDAO which attempts to verify that user against the database.
- If the user exists in the data, the model instantiates a User object and returns that object to the controller.
- The controller then tells Tomcat to load the homepage and sends the user object to the resulting jsp page.
- The view then shows a nice little “Welcome back Sue” message for the user to see. The name in the message comes from the user object.
- If the user does not exist in the database, the model returns an error and the controller instantiates the error page with an appropriate message to the user.
The general idea behind the MVC pattern is to abstract and remove as many dependencies as possible from between the layers. If done correctly, the entire web interface could be removed and a Swing ui could be put in its place with no code modifications needed on the part of the model. You may need to code up a few different controllers, but the model should not have to change. Likewise, you should be able to replace the entire model without having to change the view. This is accomplished because the point of dependency is the controller which sits in the middle.
I have listed a few links below for more information.
Reference
- Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam (SCWCD) — this is an excellent book that discusses the MVC pattern and how to implement it using JSPs and Servlets.
- Model-view-controller — Wikipedia entry on MVC
- Understanding JavaServer Pages Model 2 architecture — a JavaWorld article from a few years ago that discusses the MVC model 2 pattern
- Struts — Struts is probably the most popular MVC implementation for Java web development
- Core J2EE Patterns - Dispatcher View — a pattern in Sun’s Core J2EE Patterns that allows you to implement the MVC design pattern