Struts Framework Tutorial with Examples
Struts Framework Tutorial with Examples
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 .