Introduction to Struts Architecture
By: Daniel Malcolm in Struts Tutorials on 2007-09-13
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Handling Duplicate Form Submissions in Struts
Guidelines for Struts Application Development
Configuring JDBC DataSources in Struts
When is the best time to validate input in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?
Comments