Struts Framework in JAVA

Introduction

To date, two versions of the Struts framework have been released for general use. This kind of framework is mainly used to create MVC (Model View Controller) applications. Almost all Web-based MVC applications use this framework as a building block during development. It was founded by Craig McClanahan in 200 and later patented by Apache Foundation which released Struts 1.0 in 2001.

What is the Struts Web Framework?

Struts is an open-source web-based framework for creating state-of-the-art JAVA enterprise applications. The framework extends a lot of its functionalities from the Servlet API and follows a general Model – View – Controller architecture. The applications thus created using the Struts framework are easily maintainable and flexible when it comes to making changes to the structure or interface. Some of the standard technologies that this framework bases on are JSP pages, JavaBeans, resource bundles, and XML.

MVC Architecture

As Struts follows the basic MVC architecture, the three building blocks of struts are model, view, and controller. Therefore, it becomes very imperative to understand the functioning of the MVC to get a grip on Struts. MVC is a common software design pattern that is extensively used for applications of all types. It goes without saying there are three parts to its architecture.

  • Model – Model is the lowest tier in the architecture that is mainly responsible for data handling. Model makes direct contact with the controller and deals with query requests. The model finally sends a response to View conforming with the Controller.
  • Controller – This is the first tier in the architecture that directly handles user inputs in the form of events. The Controller also has the task of authenticating the input before it passes it on to Model to develop a response.
  • View – The View layer stands in between the Model and Controller layers. As the name suggests, this layer deals with presenting data in an organized manner. The manner in which the data will be presented is decided by the Controller and passed on to View. They are generally script-based templating systems.

Struts Architecture

Struts is a pull-MVC framework. The only difference with the conventional MVC architecture is the Action takes up an active role in Model rather than its usual position in Controller. Discussed below are the various components of the architecture and where they sit in the MVC structure.

  • As mentioned above, the Action component acts as the model here.
  • The Controller has two components to it namely the Dispatcher Filter and the Interceptors.
  • View used technologies like the JSP, Free marker, etc. View interacts with the browser with the help of Value Stack/OGNL and sends results to the event.

Request Life Cycle (Work Flow in Struts framework)

  1. After a request has been made, it is directly sent to the Controller. The Filter Dispatch of the Controller decides a favorable action and sent to the model.
  2. The chosen action is performed in Model depending upon the type of request and diagnosis done by the Filter Dispatch.
  3. Interceptors are used to post-process the request.
  4. Finally, the View prepares a result that is displayed to the user.

Example

/*We will be creating a simple application using the Struts framework. The application is that of a student register that is accessible only to the staff. Teachers can get student grades by entering the surname as well as edit if required*/

//For the purpose of this example, we will just be documenting the .java class and struts.xml class.

public class Student {
private int roll;
private String lastName;
private double grade;
public int getRoll() {
return roll;
}
public String getName(){
return lastName;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public String execute() {
return “success”;
}
}

//struts.xml

<?xml version = “1.0” encoding = “UTF-8”>
<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.1//EN” “http://struts.apache.org/dtds/struts-2.1.dtd”>
<struts>
<package name = “default” extends = “struts-default”>

<action name = “student” class = “java.Student”>
<result name = “success”>welcome.jsp</result>
</action>
</package>
</struts>

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.