Introduction to Struts Architecture

By: Daniel Malcolm Viewed: 153361 times  Printer Friendly Format    


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.



Most Viewed Articles (in Struts )

Latest Articles (in Struts)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Nice guidelines which provide to understand struts
View Tutorial          By: harshal patel at 2010-08-13 04:59:16

2. nice overview on struts.....
View Tutorial          By: priyanka at 2010-08-23 00:13:33

3. nice to understand
View Tutorial          By: priyanka at 2010-08-23 01:58:59

4. short notes to struts,but i expect more explanatio
View Tutorial          By: jayashri at 2011-11-23 18:24:07

5. A good short article on Struts. Require a step by
View Tutorial          By: Atul Saxena at 2012-01-09 19:56:59

6. it good to understand struts arch.
View Tutorial          By: i know about hibernate all type of query? at 2012-07-30 07:08:39

7. nice info on struts and very good explanation
View Tutorial          By: bharadwaj kvnss at 2013-02-07 17:18:44