Programming Tutorials

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

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Struts )

Latest Articles (in Struts)