by Dinesh Thakur

Request Initialization {HTTPSERVLETREQUEST}

A user sends a request fromits web browser which is in the form of a URL and it represents an Action. The user's request begins and ends in its web browser. The request sent by the user passes through a standard filter chain which actually makes the decision by calling the desired StrutsPrepareAndExecuteFilter. Its fully qualified name is org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.

FilterDispatcher was used instead of StrutsPrepareAndExecuteFilter in older Struts version 2.1.3

Struts 2 Servlet Filter {StrutsPrepareAndExecuteFilter}

StrutsPrepareAndExecuteFilter is the upgraded version or we use this in Struts 2.1 version. In Struts 2 the StrutsPrepareAndExecuteFilter plays the role of a controller. The preparation and execution phases of the Struts2 handle by StrutsPrepareAndExecuteFilter. This object is a servlet filter whose job is to inspect each incoming request and determine which action should handle the request. This Struts2 Servlet filter also consults the ActionMapper to determine if the request should refer to an Action or not. The framework handles all of the controller work and only needs to be informed about which request URL maps to which action. Then this information is passed to the framework using an XML configuration file i.e. struts.xml.

We use this filter rather than dispatch filter due to some reasons which are given below-:

• There were a number of issues that arise while using this FilterDispatcher and its deployment, new Filter offers a better way to enable customizations and overrides these issues.

• Make it crystal clear to developers which filters are doing which processes.

• Make dispatch process more flexible to support things like native operation in OSGL or plugin system.

         Struts Architecture

Action Mapper

The ActionMapper interface gives us a mapping between HTTP requests and action invocation requests and vice-versa. When we send an HttpServletRequest Action Mapper determines that an action should be invoked, then the StrutsPrepareAndExecuteFilter passes the control to the Action Proxy and if no action invocation request matches, then ActionMapper return null and it may return an ActionMapping which describes an action invocation for the framework to try.

The ActionMapper is not required to guarantee that the ActionMapping returned be a real action or otherwise it confirms a valid request. Accordingly, most ActionMappers do not require consulting the Struts configuration just to determine if a request should be mapped.

Just as requests can be mapped from HTTP to an action invocation, the reverse is true as well. However, because HTTP requests (when shown in HTTP responses) must be in a String format, a String is returned rather than an actual request object.

Action Proxy

The Action Proxy then consults the framework Configuration Files manager which again consults to struts.xml to determine whether any action is defined for the request and then processes the request and the execution results after the request has been processed.

Action Invocation

The Action Proxy creates an Actionlnvocation that is responsible for the command pattern implementation. Then the Actionlnvocation consults the configuration and creates an instance of Action and calls the interceptors and invokes the corresponding action. When the action returns, the Actionlnvocation looks for the corresponding result of the action in the struts.xml file and executes the result which may be JSP or templates.

Configuration Manager

The configuration files is the Web application Deployment Description or web.xml file. In Struts 2, this file plays an important role in the configuration of web application. This file gives full control to the developer for configuring Struts-based application. (When, by default Struts loads a set of internal configuration files to configure it, and then another set of configuration for configuration your application.)

The Configuration files that are used to configure an application in Struts 2 are as follows:-

web.xml

struts.xml

struts. properties

struts-default.xml

struts-plugin.xml

Struts allows dynamic reloading of XML configuration file, some configuration files can be reloaded dynamically. This dynamic reloading makes interactive development possible. Using dynamic reloading can reconfigure your action mapping of your application during development. But if you want to use this feature, you need to set struts.configuration.xml, reload copy of struts. properties file.

Actions

Actions are the core of the Struts 2 framework, as they are for any MVC (Model View Controller) framework. Each URL is mapped to a specific action, which provides the processing logic necessary to service the request from the user. This is an action-oriented framework

But the action also serves in two other important capacities.

The action plays an important role in the transfer of data from the request through to the view, whether it is a JSP or other type of result.

The action must assist the framework in determining which result should render the view that will be returned in the response to the request.

Optionally you can extend the ActionSupport class which implements six interfaces including Action interface. The Action interface is as follows

public interface Action {

public static final String SUCCESS= "success";

public static final String NONE = "none";

public static final String ERROR= "error";

public static final String INPUT = "input";

public static final String LOGIN = "login";

public String execute() throws Exception;

}

we have to extend ActionSupport class which implements action interface, so we can use String constants SUCCESS and ERROR like this.

Role of action:

• Actions provide Encapsulation:

The role of this encapsulation is to hold the business logic. Actions are invoked by calling the execute() method.

• Action helps to carry data.

• Action returns control string.

Strengths of Struts Framework:

1. Validation:

  Before executing the business logic of an application, the Input values must be validated. If the input values are correct then the business logic of the application will be executed, otherwise the request will be rejected.

In order to validate the input values given by the client. we have both client side and server side validations. Using struts we have extensive support to perform validations on the input. Struts provides client side validation by without creating any JavaScript code.

2. Internationalization (I18N)

I18N means, it is a process of converting a content of an application into multiple languages according to the language the religion and the country.

In struts framework we have inbuilt support for I18N. It means without writing any additional code the framework convert the page content according to the browser.

3. Exception Handling:

  In an application, exception can arise at various places. if you want to manually handle them , then we need to put try catch block at various places.

Writing try and catch blocks at various places makes burder to the application developer. So in struts the framework will handle the exception that occurred at various places in an application and provide an error page back to the browser rather than printing exception.