Introduction to Struts Architecture

By: Daniel Malcolm Emailed: 1586 times Printed: 2038 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This tutorial looks closely at the Struts terminology for controller servlet and Handler objects that we mentioned and understand Figure below. Since this is your first look at Struts, we will not get into every detail of the HTTP request handling  lifecycle in Struts framework. For now, let us concentrate on the basics. 

In Struts, there is only one controller servlet for the entire web application. This controller servlet is called ActionServlet and resides in the package org.apache.struts.action. It intercepts every client request and populates an ActionForm from the HTTP request parameters. ActionForm is a normal JavaBeans class. It has several attributes corresponding to the HTTP request parameters and getter, setter methods for those attributes. You have to create your own ActionForm for every HTTP request handled through the Struts framework by extending the org.apache.struts.action.ActionForm class. Consider the following HTTP request for App1 web application – http://localhost:8080/App1/create.do?firstName=John&lastName=Doe. The ActionForm class for this HTTP request is shown in the listing below. The class MyForm extends the org.apache.struts.action.ActionForm class and contains two attributes – firstName and lastName. It also has getter and setter methods for these attributes. For the lack of better terminology, let us coin a term to describe the classes such as ActionForm – View Data Transfer Object. View Data Transfer Object is an object that holds the data from html page and transfers it around in the web tier framework and application classes. 

// Sample ActionForm 
public class MyForm extends ActionForm {
private String firstName;
private String lastName;

public MyForm() {
firstName = “”; lastName = “”;

}


public String getFirstName() {
return firstName;

}

public void setFirstName(String s) {
this.firstName = s;

}

public String getLastName() {
return lastName;

}

public void setLastName(String s) {
this.lastName = s;
}
}


The ActionServlet then instantiates a Handler. The Handler class name is obtained from an XML file based on the URL path information. This XML file is referred to as Struts configuration file and by default named as struts-config.xml. 

The Handler is called Action in the Struts terminology. And you guessed it right! This class is created by extending the Action class in org.apache.struts.action package. The Action class is abstract and 
defines a single method called execute(). You override this method in your own Actions and invoke the business logic in this method. The execute() method returns the name of next view (JSP) to be shown to the user. The ActionServletforwards to the selected view. 




Struts architecture. 

Now, that was Struts in a nutshell. Struts is of-course more than just this. It is a full-fledged presentation framework. Throughout the development of the application, both the page author and the developer need to coordinate and ensure that any changes to one area are appropriately handled in the other. It aids in rapid development of web applications by separating the concerns in projects. For instance, it has custom tags for JSPs. The page author can concentrate on developing the JSPs using custom tags that are specified by the framework. The application developer works on creating the server side representation of the data and its interaction with a back end data repository. Further it offers a consistent way of handling user input and processing it. It also has extension points for customizing the framework and much more. In this section, you got a bird’s eye view of how Struts works. More details await you in the chapters ahead. But you have to install Tomcat and Struts on your machine to better understand the chapters ahead. Hence we will cover Tomcat and Struts installation briefly in the ext section.


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(6)


1. View Comment

Nice guidelines which provide to understand struts in closer aspects....

If possible please give the idea about portlet development in liferay portal with struts usage.

Thank you.


View Tutorial          By: harshal patel at 2010-08-13 04:59:16
2. View Comment

nice overview on struts.....

View Tutorial          By: priyanka at 2010-08-23 00:13:33
3. View Comment

short notes to struts,but i expect more explanation to the struts

View Tutorial          By: jayashri at 2011-11-23 18:24:07
4. View Comment

A good short article on Struts. Require a step by step tutuorial also.

View Tutorial          By: Atul Saxena at 2012-01-09 19:56:59
5. View Comment

it good to understand struts arch.

View Tutorial          By: i know about hibernate all type of query? at 2012-07-30 07:08:39
6. View Comment

nice info on struts and very good explanation

View Tutorial          By: bharadwaj kvnss at 2013-02-07 17:18:44

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Daniel Malcolm
javac options in Java
Operator Precedence in Java
Calling Multiple Listeners in JSF
Using free() Function in C
ForwardAction in Struts
Listing Files and Directories sample program in C
Binary Tree - (Self-referential Structures) example program in C
A simple program using EL in JSP
Command-line Arguments in C
Example Calculator program in C - describing use of External Variables in C
Assignment Operators and Expressions in C
The for statement in C
JSF Basics
assert() Versus Exceptions in C++
RMS Basics in J2ME

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling Duplicate Form Submissions in Struts
What is Struts? Which Version of Struts to use?
ForwardAction in Struts
ActionErrors and ActionError in Struts
DispatchAction in Struts
Struts and Tiles - Steps to use Struts and Tiles
Configuring JDBC DataSources in Struts
Using JavaScript to submit a form in Struts
Struts 1 vs Struts 2
MVC Architecture (Model 2 Architecture)
Handling multiple buttons in HTML Form in Struts
Writing the first Struts application
When is the best time to validate input in Struts
Model 1 Architecture
Most Emailed Articles (in Struts)
Downloading and installing Struts
The directories and files of a Struts application
Editing web.xml in a Struts Application
Editing struts-config.xml in a Struts Application
Creating the first application using Struts 2
Model 1 Architecture
MVC Architecture (Model 2 Architecture)
Installing and configuring Tomcat and Struts
Using Checkbox & Radio Tags, html:select, html:options in Struts Forms
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
ForwardAction in Struts
Guidelines for Struts Application Development
What is Struts? Which Version of Struts to use?
History of Struts
MVC with configurable controller