0% found this document useful (0 votes)
25 views2 pages

Struts Framework Tutorial with Examples

The document summarizes how the Struts framework works behind the scenes. It describes the main steps as: 1. The ActionServlet loads on startup and reads the Struts configuration file. It handles requests matching the *.do pattern. 2. The ActionServlet delegates requests to the RequestProcessor, which looks up the configuration for the URL and populates and validates any form objects. 3. If validation passes, the RequestProcessor executes the associated action class and forwards the request based on the return value to the success or failure page defined in the configuration. This completes the Struts request processing flow.

Uploaded by

Vijay Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Struts Framework Tutorial with Examples

The document summarizes how the Struts framework works behind the scenes. It describes the main steps as: 1. The ActionServlet loads on startup and reads the Struts configuration file. It handles requests matching the *.do pattern. 2. The ActionServlet delegates requests to the RequestProcessor, which looks up the configuration for the URL and populates and validates any form objects. 3. If validation passes, the RequestProcessor executes the associated action class and forwards the request based on the return value to the success or failure page defined in the configuration. This completes the Struts request processing flow.

Uploaded by

Vijay Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Struts Tutorial -- Code Examples

Struts Flow-How Struts Works?

Struts Flow start with ActionServlet then call to process() method of RequestProcessor.

Step 1. Load ActionServlet using load-on-startup and do the following tasks.

Any struts web application contain the ActionServlet configuration in [Link] file. 
On load-on-startup the servlet container Instantiate the ActionServlet . 
First Task by ActionServlet : The ActionServlet takes the Struts Config file name as an init-param. 
At startup, in the init() method, the ActionServlet reads the Struts Config file and load into memory. 
Second Task by ActionServlet : If the user types [Link] in the
browser URL bar, the URL will be intercepted and processed by the ActionServlet since the URL has a
pattern *.do, with a suffix of "do". Because servlet-mapping is 
<servlet-mapping> 
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Third Task by ActionServlet : Then ActionServlet delegates the request handling to another class
called RequestProcessor by invoking its process() method.

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>[Link]</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/[Link]</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

Step 2. ActionServlet calls process() method of RequestProcessor.

The RequestProcessor does the following in its process() method:


a) The RequestProcessor looks up the configuration file for the URL pattern /submitForm (if the URL is
[Link] and and finds the XML block (ActionMapping).

ActionMapping from [Link]

<action path="/submitForm"
type="[Link]"
name="EmpForm"
scope="request"
validate="true"
input="[Link]">
<forward name="success"
path="[Link]"/>
<forward name="failure" path="[Link]" />
</action>

b) The RequestProcessor instantiates the EmpForm and puts it in appropriate scope – eithersession or


request. 
The RequestProcessor determines the appropriate scope by looking at the scope attribute in the same
ActionMapping.
c) RequestProcessor iterates through the HTTP request parameters and populates theEmpForm.
d) the RequestProcessor checks for the validateattribute in the ActionMapping. 
If the validate is set to true, the RequestProcessor invokes the validate() method on
theEmpForm instance. 
This is the method where you can put all the html form data validations. 
If Validate fail the RequestProcessor looks for the input attribute and return to JSP page mentioned
in input tag. 
If Validate pass goto next step. 
e) The RequestProcessor instantiates the Action class specified in the ActionMapping (EmpAction) and
invokes the execute() method on the EmpAction instance.

signature of the execute method is

public ActionForward execute(ActionMapping mapping,


ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
//your logic
return [Link]("success");
}

f) In return [Link]("success") 


RequestProcessor looks for the success attribute and forward to JSP page mentioned insuccess tag. i.e
[Link]. 
In return [Link]("failure") 
RequestProcessor looks for the failure attribute and forward to JSP page mentioned in failuretag. i.e.
[Link]

Common questions

Powered by AI

In Struts, data validation is handled by the RequestProcessor based on the 'validate' attribute in the ActionMapping . If set to true, the RequestProcessor calls the validate() method on the form bean instance, where developers implement form data validations . If validation fails, the RequestProcessor checks for an 'input' attribute in the ActionMapping and forwards the request back to the specified JSP page for corrections, allowing users to rectify their input data .

The 'scope' attribute in ActionMapping is crucial for defining the lifecycle and availability of form bean instances within a Struts application. It determines where the form bean is stored during request processing, which can be either in the 'request' scope or 'session' scope . When set to 'request', the form bean is available only for the duration of the request, meaning it cannot maintain state across multiple requests. Conversely, setting it to 'session' ensures the form bean's state is preserved between requests, allowing for more complex interactions where continuity of information is required. This attribute allows developers to manage memory usage and data persistence according to the specific needs of the application, influencing how subsequent requests are handled .

Struts leverages the servlet container's initialization lifecycle to prepare the core components of the framework at startup. The 'load-on-startup' setting in the web.xml file triggers the initialization of the ActionServlet when the web application starts . During this process, the ActionServlet reads the Struts configuration file specified in init-params and loads it into memory, thus configuring the framework's request handling parameters and mappings . This early initialization is crucial as it ensures that the ActionServlet is ready to intercept requests and delegate them appropriately without delay when they first arrive, ensuring efficient processing and reducing runtime overhead .

The execute() method in an Action class is a central part of request processing in the Struts framework. This method is invoked by the RequestProcessor as part of handling the user's request after successful validation . The method takes four parameters: ActionMapping, ActionForm, HttpServletRequest, and HttpServletResponse . Within execute(), developers implement the logic necessary to process the request, interact with model components, and determine which view component (such as a JSP page) should be rendered next. This is achieved by returning an ActionForward object that corresponds to 'success' or 'failure' outcomes specified in the ActionMapping .

ActionMapping in a Struts application serves as a configuration data structure that maps URL patterns to their corresponding Action classes and form beans. It is critical for routing requests because it defines how requests are processed in terms of which form bean and Action class should be used based on the URL . The ActionMapping specifies the path, type (class name), form bean name, scope, and validation behavior, along with the outcome paths for 'success' and 'failure'. Upon receiving a request, the RequestProcessor consults the ActionMapping to determine how to instantiate and populate form beans, which Action class to execute, and which view components to forward the process based on the logic defined within these mappings .

Upon receiving a URL request, the RequestProcessor's first step is to consult the configuration file to find the corresponding ActionMapping for the URL pattern, such as '/submitForm' . It then instantiates the form bean specified in the ActionMapping, placing it in the scope defined there, either session or request . The processor populates this form instance with HTTP request parameters . If the ActionMapping's 'validate' attribute is set to true, it invokes the form's validate() method for form data validations. On validation failure, it forwards to the JSP page specified by the 'input' attribute. If validation passes, the RequestProcessor instantiates the specified Action class and calls its execute() method to process business logic, then forwards to a JSP defined by the 'success' or 'failure' attribute in the ActionMapping .

In the Struts framework, the ActionServlet and RequestProcessor work in tandem to handle incoming requests. The ActionServlet, upon initialization, is responsible for setting up the web application's configuration parameters by reading the Struts configuration file, which dictates the URL patterns and their associated components . Its role in request handling begins when a URL request with a '.do' pattern is intercepted . At this point, the ActionServlet delegates the request to the RequestProcessor, signaling it to call its process() method . The RequestProcessor then systematically follows the ActionMapping configurations to determine the correct form bean and Action class associated with the URL, handles form validations, and executes the application logic before forwarding the request to the appropriate JSP . This division of responsibilities allows for organized processing and separation of concerns within the framework..

The initial role of the ActionServlet in a Struts-based web application is to load and initialize using the 'load-on-startup' configuration from the web.xml file . It reads the Struts configuration file specified as an 'init-param' during the initialization phase . The ActionServlet determines which URLs to process by intercepting requests that match a specific pattern defined in the servlet-mapping of web.xml. In this case, it processes URLs that have the suffix '*.do' .

The Struts framework facilitates loose coupling between components primarily through its use of configuration files and well-defined contracts between different layers. The use of the struts-config.xml file enables separation of component configurations from the application logic, as it specifies how URLs map to form beans and Action classes without embedding these details in the code . Furthermore, Action classes utilize a standard method signature (execute() method) to interact with the framework's infrastructure, allowing developers to focus on business logic while abstracting away the details of request handling. The framework's reliance on interfaces and base classes (such as ActionForm and Action) also helps ensure that components are substitutable and configurable, promoting modularity and ease of maintenance .

When handling a form submission, the RequestProcessor follows several steps: First, it looks up the ActionMapping associated with the URL to find settings related to '/submitForm' . It then instantiates the EmpForm bean and places it in the 'request' or 'session' scope as specified . Next, it iterates through the incoming HTTP request parameters, populating the EmpForm fields accordingly . If the 'validate' attribute in the ActionMapping is true, the processor calls the form's validate() method to ensure data integrity . Assuming validation passes, the RequestProcessor instantiates the EmpAction class, executing the business logic encapsulated in its execute() method, which involves returning an ActionForward that points to either a 'success' or 'failure' outcome depending on the business processing results .

You might also like