04 - Versions of Struts

Introduction:

  • Struts framework was launched by Apache in the year 2000. Since then struts 1 version was running very well. But to meet the current product demands and requirements, the struts 2 version was launched and thereafter, recently, Apache no longer supports struts 1 version.

  • There are two versions of Struts framework: struts 1.x and struts 2.x. Here ‘x’ stands for more subversions of struts.

  • This chapter will cover the major differences between both these versions and for detailed descriptions and examples you can refer next chapters of this tutorial.

Differences between Struts 1.x and 2.x:

  • The main difference between struts 1.x and struts 2.x is the configuration file. In struts 1.x version, the configuration is named as struts-config.xml where as in struts 2.x version it is named as struts.xml

  • In struts 1.x version, for every page, multiple tag libraries have to be included. That is, there are different tag libraries of including the functionality of HTML, Beans, Logic, etc. Whereas in struts 2.x version, only one tag library encapsulates the functions of all the individual libraries. So there is only one include statement here.

 

In struts 1.x :

<%@ taglib uri = "/WEB-INF/struts-html.tld" prefix = "html" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix = "logic" %>

<%@ taglib uri = "/WEB-INF/struts-bean.tld" prefix = "bean" %>

            In struts 2.x :

<%@ taglib uri = "/struts-tags" prefix = "s" %>

 

  • Mutual exclusion was a problem in struts 1.x version. The classes responsible for Action are supposed to look after resource allocation in a proper manner. That is, the respected operations are to be coded in thread-safe manner. In other words, either synchronized methods or synchronized blocks (from core Java) are to be used. While in struts 2.x version, thread-safety is not an issue.

  • In struts 1.x, the Java beans and Action classes are separately defined. That is, for each Action class, the variables used inside it are defined (using getter and setter methods) in separate beans class. While in struts 2.x version, Java beans and action classes are combined together.

  • Struts 1.x is servlet dependent. The reason is, the Action classes of struts 1.x version have to extend org.apache.struts.action.Action class and provide implementation of execute() method. While in case of struts 2.x framework, the Action classes have flexibility in implementing the Action interface or extending ActionSupport class. Both the interface and the class have execute() method which becomes optional to define if used by extending the ActionSupport class.

 

In struts 1.x:

import org.apache.struts.action.*;
class calculate extends Action
{
  //body of class
    public String execute()
    {
     // body of method
    }
}

In struts 2.x:

import org.apache.struts.action.*;
class calculate extends Aciton
{
    //body of class
} 

OR

class calculate implements ActionSupport
{
  //body of class
  public String execute()
  {
   // body of method
  }
}
  • In Struts 1.x framework, front end is totally controlled by the ActionServlet whereas, in Struts 2.x, Filters can be used as a front end controller.
  • JSTL’s (JSP standard tag library) EL is used as an expression language in struts 1.x while struts 2.x used OGNL (Object Graph Notation Language) for expression language.
  • In struts 1.x, validations are performed in ActionForm manually. Where as in struts 2.x version, XWork Validator is used which provides validations for some specific fields automatically.
  • The main drawback of using struts 1.x framework is that all the request processors (action handlers) follow the same life cycle. While struts 2.x framework is designed in such a way that all the interceptors follow a different life cycle. That is struts 2.x actions can be customized and thus it is more flexible and feasible to use.

 

Easy to remember the differences between Struts 1. x and 2. x:

  •  

Struts 1.x

Struts 2.x

Configuration file

Named as struts-config.xml

Named as struts.xml

Tag Library

Three tag libraries are required:

<%@ taglib uri = "/WEB-INF/struts-html.tld" prefix = "html" %>

 

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix = "logic" %>

 

<%@ taglib uri = "/WEB-INF/struts-bean.tld" prefix = "bean" %>

Only on tag library encapsulates all the features:

<%@ taglib uri = "/struts-tags" prefix = "s" %>

  •  

Thread-safe methods should be declared.

Thread-safety is not an issue.

Co-ordination of Beans and Action classes

Java beans and action classes are separately defined

Getter-setter methods can be added in action classes.

Servlet dependency

Servlet dependent

Servlet independent

Extending and implementing actions

Action classes need to extend org.apache.struts.action.Action

Action classes can implement Action interface or can also extend ActionSupport class

Execute() method

Action classes have to provide body of execute() method

Implementing execute() method is not a compulsion.

Front end

Front end  is controlled by ActionServlet

Front end  is controlled by Filters

Expression language

JSTL’s EL (Expression language) is used here

For EL, OGNL (Object Graph Notation Language) is used.

Validations

Validations are to be performed in ActionForm manually

Validations are handled by XWork Validator which provides validations of certain fields automatically.

Life cycle

All the request handlers follow same life cycle.

nterceptors in struts 2.x does not follow same life cycle.

 

Like us on Facebook