Article
Complete the MVC Puzzle with Struts
The MVC pattern is a laudable ideal, but those who have implemented it on more than one site have probably felt like they were reinventing the wheel in several areas. The open-source Apache Struts framework lets you get on with the job of building your site, as it provides the best mag wheels in town.
In this article, we’ll take a brief trip into the world of the Model-View-Controller (MVC) design pattern, and, in particular, we’ll look at how it is implemented using the Struts framework. We will start by describing the MVC pattern at a theoretical level, then move on to describe how we might ‘roll-our-own’ MVC framework. Once we’ve looked at this, we’ll describe Struts and show how this technology helps us quickly and simply build MVC-based Web applications.
The Model-View-Controller Pattern
The MVC pattern was first conceived in the Smalltalk world. We’ll bypass this history for now, and instead concentrate on how it’s applied in the realm of Java Web application development.
When Java Servlets were first conceived, programmers instantly recognised them as a great technology. They were faster, more flexible, more reliable, and more powerful than the existing CGI method of Web programming. However, there was one big flaw with developing Servlet based Web applications -- the constant need for out.println(“…”) type statements to output HTML to the user’s browser. This method was error prone, and extremely time consuming (developers have to escape any quotes etc. that are sent to the browser). It also made it very difficult to modify the presentation of a Website, as the presentation and logic were all bundled up together in one horrible mess.
The answer to this was JavaServer Pages, which turned Servlets on their head. With this, we wrote our business logic code in among our HTML using a series of <%…%> tags. This made our applications JSP-centric, which, despite not being quite as bad as their Servlet-centric counterparts, was still messy and necessitated a lot of extra code to control the flow of the application. Really, this had no place among the formatting code in a JSP. Cleary it was time to find another way.
It was soon realised that JavaServer Pages and Servlets could work very well together. After all, Servlets are very good at logic-type programming, processing requests, and dealing with flow of control, while JavaServer Pages are very good at formatting the results of request processing, and collecting user input via the browser. This way of working soon became known as Model 2 (using JavaServer Pages or Servlets alone is known as Model 1).
Model 2 was not particularly innovative or new; many people soon realised that it follows the well-known MVC pattern that was developed back in the days of Smalltalk. As such, Java programmers tend to use the terms Model 2 and MVC interchangeably.
What is Model-View-Controller?
Now that we know a little about the history of MVC in the development of Java Web applications, it seems sensible to describe the pattern itself in more detail. In this section, we’ll find out exactly what Models, Views, and Controllers actually are, what their responsibilities are, and how we might implement a simple framework ourselves. Let’s start by looking at how the Model, the View, and the Controller interact with one another:

Figure 1 : Model 2/MVC architecture
As you can see from the above diagram, the user interacts with the Controller components (usually represented by Servlets) by submitting requests to them. In turn, the Controller components instantiate Model components (usually represented by JavaBeans or other similar technology), and manipulate them according to the logic of the application. Once the Model is constructed, the Controller decides which View (usually represented by JavaServer Pages) to show to the user next, and this View interacts with the Model to show the relevant data to the user. The View can also modify the state of the Model before it is submitted to the Controller for the process to start again.
In order to more fully understand the interactions between the components, it is useful to consider a simple example of such a framework. This example will look at a simple application that submits and records users’ login information. Don't forget to download the code for this article here.
Sam is a Java Developer and Architect based in London. He's co-authored 3 books so far (Professional Servlets and Professional SCWCD Certification from Wrox Press, and Pro JSP 2.0 from APress!). Visit him at