What Is MVC?

MVC is an acronym for the Model View Controller architecture. The purpose of this pattern is to simplify the implementation of applications that need to act upon user requests and manipulate and display data. There are three distinct components of this pattern:

  • Model represents data that the user is expecting to see. In most cases, the model consists of Java beans.

  • View is responsible for rendering the model. A view in a text editor probably displays the text in appropriate formatting; in most cases, a view in a web application generates an HTML output that the client's browser can interpret.

  • Controller is a piece of logic that is responsible for processing and acting upon the user requests, building an appropriate model, and passing it to the view for rendering. In case of Java web applications, the controller is in most cases a servlet. Of course, the controller can be implemented in any language a web container can execute.

Currently, there are two models of MVC. In the domain of web applications, Model 1 architecture can be illustrated using the diagram in Figure 17-1.

image from book
Figure 17-1: MVC type 1 architecture

As you can see, the JSP pages are in the center of the application. They contain both the control logic and presentation logic. The client makes a request to a JSP page; the logic in the page then builds (typically plain old Java objects) and renders the model. The separation of the presentation layer and control layer is not very clear. In fact, with the exception of "Hello world" applications, Model 1 quickly grows out of control, simply because of the amount of logic that different JSP pages need to perform.

Model 2 is far more manageable in a larger application. Whereas in Model 1, a JSP page was both the view and controller, in Model 2, there is a separate JSP page for the controller.

It is now the controller that intercepts the user requests, prepares the model, and passes it to the view for rendering. The JSP pages no longer contain logic for processing the requests; they simply display the model prepared by the controller.

We used JSP in place of the view and controller in Model 1 and the view in Model 2. This is obviously incorrect because the view is not limited to just JSP pages. Spring MVC architecture is an implementation of Model 2 MVC, hence the view can be anything that can render the model and return it to the client.

Now that we have covered the basics of the MVC pattern, we can explore how this pattern is implemented in Spring and how you can use it in your web applications.

image from book
Figure 17-2: MVC type 2 architecture



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net