Apache Struts Framework: The Big Picture
A framework for Web development is a set of related classes, utilities, and modules that simplify a project's creation by providing pre-build parts. In this article, I will focus exclusively on Apache Struts J2EE framework, even though a cornucopia of other frameworks exists for any imaginable development problem. Struts is extremely flexible and is a good fit for most Web-based projects; however, if you find that, after reading this article (and perhaps a few other books on Struts), it is not a 100 percent match for your project, please look at another viable framework. I have described several in my previous article "Modern Java Frameworks for Web Development". I have also described WebWork framework in more detail in a separate article, "Working with the WebWork Framework". The list of some other frameworks is provided in the references section.
In this article, I will describe how to work with Struts, go over its main features, and discuss setting it up in an enterprise development environment, such as JBuilder 2005.
Overview of Struts
Struts became very popular as an open source framework/project because it solved a lot of problems and provided a higher level of abstraction for application developers. In addition to a very stable framework, Struts provides plug-in capability, among other things. Developers who use Struts don't need to implement a lot of common functionality because it's already provided for them. The structure of the framework also forces any project to adhere to the Model-View-Controller (or Model2) paradigm. For example, provided componentssuch as main Servlet controller, abstract and concrete action classes to inherit from (for the model layer and actions), numerous utilities and tags, as well as user data validationall enforce MVC structure. This is particularly beneficial for novice Web developers who may have tendencies to embed model logic in the presentation layer or GUI elements in the model layer.
The Struts framework is an open-source product for building Web applications based on the model-view-controller (MVC) design paradigm. It uses and extends the Java Servlet API and was originally created by Craig McClanahan. Please note: Because of the extensiveness of the Struts framework, there are entire books written just about it. It will be impossible to describe all the features of the framework in the constraints of this article. However, I will talk about the most important aspects of the framework. Also, I am assuming that you are familiar with some enterprise Integrated Development Environment (IDE), such as JBuilder 2005 or Eclipse and know how to create a Web application with it.
Any application that wants to use Struts components must include its libraries. The project must include struts jars as well as any other libs such as third-party database connection drives.
At the heart of Struts is a struts-config.xml file that is located in the WEB-INF directory of your project and describes everything about the coordination of requests and responses for the application. It also describes form bean classes, database connection data-sources, action mappings, instructions in the event of error for each action, and plug-ins (such as validation, to which I will get in a moment).
This config file must be included in you application, and it can be created by hand or with an IDE. For example, JBuilder provides a very user-friendly GUI Struts Config Editor. See below.
Struts Config Source Example Code
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation// DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <data-sources> <data-source type="com.mysql.jdbc.jdbc2.optional. MysqlDataSource"> <set-property property="url" value="jdbc:mysql://localhost/database" /> <set-property property="user" value="root" /> <set-property property="password" value="root" /> <set-property property="maxCount" value="5" /> <set-property property="driverClass" value="com.mysql.jdbc.Driver" /> <set-property property="minCount" value="1" /> </data-source> </data-sources> <form-beans> <form-bean dynamic="no" name="Login" type="com.entity.SomeClass" /> </form-beans> <action-mappings> <action path="/someAcion2" type="com.action.ScheduleEntryAction"> <forward name="success" path="/SomeView.jsp" /> </action> <action input="/Login.jsp" name="Login" path="/addUser" scope="session" type="com..action.AddAction" validate="true"> <forward name="success" path="/someAction1.do" /> <forward name="error" path="/Error.jsp" /> </action> <action path="/deleteUser" /> </action-mappings> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" /> </plug-in> </struts-config>
The Struts framework provides a controller servlet as well as the skeleton classes for actions that developers need to extend and implement the "execute" method.
The actions and outcome of the request and response can be mapped out graphically in JBuilder as well. For example, here is an action (class that extends org.apache.struts.action.Action) and flow of logic that it involves.
Similar graphical editors exist for other parts of Struts application in JBuilder as well.
The main controller and the reference to the struts config file must be placed in the standard web.xml config file for whichever application server is used.