The Struts Framework is a Java-based technology that allows Web application developers to take advantage of object-oriented design, reusable code, and “write once, run anywhere” functionality. Struts provides a framework for creating Web applications that abstracts the backend code of your applications from the display, or presentation, of your data.
Struts emerged in May 2000 as part of the Jakarta Project, which is operated by the Apache Foundation. The creators wanted to leverage Java Server Pages (JSPs), servlets, tag libraries, and object-oriented design. Before delving into Struts, you should be comfortable writing JSPs and Java code and have some familiarity with using design patterns, especially Model-View-Controller (MVC). The learning curve for Struts can be steep, but once you master it, you have a useful design tool for developing large-scale Web applications.
This is the first installment of a two-part series on the Struts Framework.
Show me the MVC
Struts is an implementation of the MVC design pattern. The MVC design pattern provides a foundation for building extendable, reusable code. Part of the attraction of the MVC is that it forces you to abstract your code and think of your project in parts-presentation, “work horse code,” and the orchestration of the two.
The "model" aspect of the pattern refers to the code that gets the real work done. For many Web applications, functionality is more important than aesthetic concerns. By keeping the model separate from the interface, code is kept manageable and reusable. For example, if you developed an insurance application, the model would be the code that does all of the hard work of processing claims, calculating premiums, and interacting with the database. The model aspect is often referred to as the "business logic."
The "view" is the interface. With the MVC pattern, the assumption is made that the interface should do very little. Certainly, it should be functional and follow usability guidelines, but the interface shouldn’t process any data. In fact, each piece of the interface should contain only enough logic to collect data and pass it on for processing. The theory is that if you have all your logic stored in the model, you can create numerous views, meaning interfaces for desktop applications, inventory systems, or Web applications. Because all the hard-working code is stored in the model, creating new views is painless, and the possibility of software coding errors is reduced.
The "controller" orchestrates interaction between the model and the view. It determines which view should be returned to the user, the validity of the information entered, and which part of the model should process input.
For more on the MVC
For more information about the MVC pattern, check out Sun’s site, which offers a nice introduction to the pattern.
Now that we have introduced MVC, let’s download the Struts Framework and look at how Struts uses the MVC design technique.
Getting the Struts Framework
The Struts Framework is available on the Web at the Jakarta Project Web site. Although the code is at version 1.0, it is very stable and mature. Like all projects from the Apache Foundation, the Struts Framework is open source, so you can grab the source and compile it yourself, or you can download the binary form. Most users won’t need the source code, but it is nice to know it is available.
The Struts distribution ships with a set of examples, all packaged as Web Application Archive (WAR) files for deployment on your application server. If you want to create your own applications with Struts, you’ll find everything you need in the Struts lib directory. In this directory, a Java Archive File (JAR) file contains all the code for the framework and all the definitions for the tag libraries. When you’re creating a new application, you’ll need to ship those files in your WEB-INF directory for deployment. We’ll cover how to do that in a future article; for now, you just need to know where they are.
MVC and the Struts Framework
Let’s examine how the Struts Framework uses the MVC pattern for creating Web applications.
Struts relies on JSP to act as the view for the MVC. Each view is a JSP page using custom tag libraries provided by the Struts Framework. All form elements are coded with the custom tags so that they can easily interact with the controller. Each form then points to a specific entry point into the controller by mapping requests from the JSP to a servlet.
The controller is a Java servlet, provided by the Struts Framework, which interacts between the view and the model. Each entry point into the controller is specified in a configuration file named struts-config.xml. This file maps requests from the view to a specific java class for processing. The controller also specifies where the next view will be.
Java beans represent the model. For each entry point into the controller, a bean is specified to store the information retrieved from the view and a Java class known as an "action class" isspecified to take action on the form input. The action class holds the business logic.
That does it for the introduction to the Struts Framework and its relationship with the MVC. The second part of the series will walk you through creating an application using Struts.