By: Fanny Ong Printer Friendly Format
Craig McClanahan originally wrote the Struts framework, mostly while on his Memorial Day vacation in 2000! The framework was subsequently donated by Craig to the Apache Software Foundation. The fact that it was mostly written in one weekend should suggest to you that it’s a very simple framework.
You should also note that anything that can be done with JSP pages and servlets can still be done with Struts. So you can still write regular servlets that extend HttpServlet in your application or have JSP pages with whatever embedded scriplet code you want. On the other hand, Struts does try to encourage certain better practices for coding web applications, and it does make it easier to write and deploy them. For instance, when using Struts, you don’t need to write a whole slew of request.getParameter() calls in your servlet to get all your form’s values. Struts does this for you with an ActionForm, which handles the population and grabbing of an HTML form’s values.
A web framework provides a set of services that can be used and reused across many different web applications. Struts is an open-source web framework that is based upon the tried-andtrusted MVC design pattern. Its core is made up of Java servlets, JavaBeans, resource bundles, XML, and tag libraries. Struts provides the following services:
A controller servlet
Ready-made tag libraries
A framework for internationalizing messages
A generic error and exception-handling mechanism
File upload and logging utilities
So how do these ActionForms fit into the overall Struts architecture? Figure below shows a schematic of a simple Struts application in general.
First, you can see that we’ve assumed that you’re using JSP pages for the view, and that these are built using the Struts tag libraries. Each client request for the application to perform a particular action is passed to the controller servlet (known as the ActionServlet), which dispatches to an Action class.
Each Action class extends org.apache.struts.action.Action. Every Action provides application-specific implementation by overriding the execute() method. The business logic needed to perform each action (process each type of request) is present in this corresponding Action class in the execute() method. The Action classes represent the controller logic for your application. They control which view elements are presented to the user. For instance, if an error occurs, the action is responsible for displaying errors on an input page.
There also may be a corresponding ActionForm JavaBean class for each action. The ActionForm class extends org.apache.struts.action.ActionForm and overrides the validate() method. Each ActionForm validates user data and can be used by the Action class to retrieve user data. The ActionForm classes represent the data of the model. The model is your data and the code you write to retrieve, save, or delete that data.
So, as you would expect from the controller, the ActionServlet provides the link between the view and the Action and ActionForm model components. To do this it needs to map the request data to the appropriate Action and ActionForm classes, depending upon the action requested by the client. The correct mapping of request actions to Action and ActionForm classes is defined in a configuration file, struts-config.xml (defined by http://struts.apache.org/dtds/struts-config_1_2.dtd). This file defines a set of ActionMapping objects that each represents a mapping of an action to the appropriate model classes. Therefore, the ActionServlet checks this struts-config.xml file to find the appropriate mapping for the current request.
Finally, if the Action class processes the request correctly, it indicates to the ActionServletwhere the user should be forwarded to (usually a JSP page), by passing the ActionServlet an ActionForward object.
For more in-depth information about the architecture and implementation of the Struts framework, you can try pointing your browser to the following links:
• http://jakarta.apache.org/struts/: Struts homepage (you can download the latest version from here)
• http://struts.apache.org/userGuide/index.html: Struts user guide
As you may have spotted already, a big benefit of using Struts is that you implement many proven Java EE design patterns without even knowing it. The Front Controller (ActionServlet and Action), View Helper (Action), Composite View (the Tiles framework that we’ll discuss later), Service to Worker (ActionServlet to actions), and Dispatcher View (ActionForward) are all integrated into the Struts framework.
Most Viewed Articles (in Struts )
Latest Articles (in Struts)
Comment on this tutorial
- Data Science
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews