Top 25 Struts Interview Questions and Answers for 2024

In this interview preparation blog post, we will cover some commonly asked Struts interview questions and provide an in-depth overview of Struts, covering its key concepts, architecture, features, and advantages to help you ace your next interview. Whether you are a beginner looking to break into the field or a professional developer wanting to sharpen your skills, preparing for Struts interview questions is essential. Let’s dive in and get you ready for success!

Basically, Struts is a powerful framework used in Java web development, offering a structured approach to building web applications. Understanding Struts is crucial for Java developers aiming to excel in web development interviews, and mastering it can open up new opportunities in your career.

So to help you and make you more confident, we have compiled a list of common Struts interview questions for beginners and professionals.

Table of Content

  • Struts Interview Questions for Freshers
  • Struts Interview Questions for Intermediate
  • Struts Interview Questions for Experienced

Basic Struts Interview Questions for Freshers

Looking to kick-start their Struts journey can benefit from mastering these fundamental interview questions. Covering topics such as Struts architecture, action classes, and form handling, these questions will help you build a solid foundation in the framework.

1. What is Struts?

Struts Framework is an open-source and easy tool to build Java Enterprise Edition (Java EE) web applications using Struts.

  • Struts framework follows the MVC pattern (model, view, and controller).
  • Also, it separates the application logic i.e. the model, the user interface i.e. the view, and the data flow between them i.e. the controller.
  • It supports and provides the Java Servlet API. Struts simplifies the development process and encourages clean code design.
  • Its primary goal is to enhance efficiency by dividing responsibilities, allowing developers to manage database interactions, present HTML pages to users, and handle data exchange easily.

For more details on Struts please refer to this article: Introduction and Working of Struts Web Framework

2. What are the key components of the Struts framework?

The components of the Struts framework are listed below:

  • Java Servlets: It handles HTTP requests and responses in Java web applications.
  • JSP (Java Server Pages): Using Java code, it dynamically generates HTML content.
  • Action Class: This class interacts with the model and implements business logic in Struts application.
  • Custom Tags: These tags used to define custom HTML tags for reusable code in JSP pages.
  • Struts Configuration File: The Struts configuration file i.e. struts-config.xml configures actions and URLs in Struts applications.

3. What are the features of Struts?

There are so many features of Struts. Some of them are listed below:

  • MVC Architecture Support
  • Supports POJO-based actions
  • Form-based Input Handling
  • Centralized Configuration
  • Tag Libraries
  • Internationalization (I18N) support
  • Supports Integration with other Technologies

For more details please refer to this article: Struts 2 Features

4. What are the core classes of a Struts application?

  • Action Servlet: This is a central controller and it handles incoming requests and dispatches those requests to appropriate action classes.
  • Action Class: This class implements the business logic to handle specific request and generates appropriate response.
  • ActionForm: It encapsulates the data from the HTML forms and then validates them by providing the bridge between the view and model.
  • ActionMapping: It maps all the incoming requests to appropriate action classes.
  • ActionForward: After executing the action, it defines the logical outcome and forwards the flow to the next view.
  • ActionMessages: During Action Execution, messages containers generated and it is used to display the feedback to the end users.
  • ActionErrors: During Form Validation, container for error messages generates. It facilitates error handling.

5. What is MVC architecture in the Struts framework?

MVC is an architectural design pattern. It stands for Model, View, and Controller.

  • Model represents the data for storing, manipulating, and retrieving purpose. In Struts, model contains all the business logic and interacts with the storage.
  • View represents presentation of the data. In Struts, JSP (Java Server Pages) implements the view layer.
  • Controller interacts between model and view. In Struts, the ActionServlet does the controller function by handling incoming requests and provides appropriate responses.

6. What is an Interceptor and what are its lifecycle methods?

Interceptor is an object and it is used to be invoked during the preprocessing and postprocessing of a request. It performs exception handling, validation, internationalization, and also it is pluggable. The lifecycle methods of interceptors are:

  • public void init(): This method id used to set up the interceptor and during initialization, it is called only once.
  • public void intercept(ActionInvocation ai): It defines how all the requests are being processes. It is executed for each request. If it returns a String, it invokes the result page, else, it calls the next interceptor or action by using the invoke() function.
  • public void destroy(): It is used only once to destroy the interceptor.

7. What is the Flow of Requests in Struts-based applications?

The Struts-based application follows MVC architecture. The flow of the requests in Struts explained below:

  • The user interacts with the application by sending the request to the application server.
  • Then, user’s request is intercepted to the controller by the controller servlet i.e. ActionServlet which is configured in web.xml
  • The controller is responsible to map the request URL to the appropriate Action class.
  • Action class is responsible for calling the function in Model, in which all the necessary business logic has executed and it generates a response, then forwards it to the appropriate view.

8. Differentiate between Struts 1 and Struts 2.

Features

Struts 1

Struts 2

Architecture

It supports monolithic architecture.

It supports modular architecture.

Configuration

The configuration is XML based configuration and file name can be any [name].xml

Configuration file name must be struts.xml

Action Handling

Action classes is not POJO actions, we need to inherit the abstract class.

Action class is POJO actions.

Tag Libraries

It has Struts custom tag library.

It has custom HTML tags and JSP Standard Tag Library (JSTL).

Request Processing

While processing the requests, it uses the concept of RequestProcessor class.

While processing requests, it uses the concept of Interceptors.

9. What is the role of the Action class in the Struts framework?

In Struts, the Action class acts as a Controller component and it is responsible for processing the requests of users. It performs the following roles:

  • The Action class receives the incoming requests from users and handles those requests.
  • Then it uses appropriate Model which contains all the business logic to process the requests.
  • After processing the requests, the Action class provides appropriate response such as redirecting to the URL or rendering a view page.
  • Then the mapping is done to the specific URLs. It ensures that the correct Action is invoked with each incoming requests.

Struts Interview Questions for Intermediate

Intermediate Struts developers can improve their skills by answering these advanced interview questions. These questions will test your knowledge and prepare you for senior-level positions, covering topics such as Struts setup, tile configuration, and custom tag libraries, as well as more complicated scenarios such as handling exceptions and constructing custom validators.

10. What is ActionForm in Struts and how it is created?

ActionForm in Struts is a JavaBean class. It acts as a container and transfers data between the presentation layer i.e. the HTML forms and the business logic layer i.e. the Action classes. It represents a collection of data from an HTML form and it provides getters and setters to get this data.

To create an ActionForm in Struts:

  • Create a JavaBean Class: Develop a Java class that represents the form data. This class contains private member variables along with corresponding getter and setter methods.
  • Extend ActionForm: We need to extend the org.apache.struts.action.ActionForm class in a JavaBean class to inherit the standard Struts function.
  • Configure in struts-config.xml: Declare the ActionForm in struts-config.xml configuration file. Also, we need to specify its fully qualified class name.
  • Use in Actions: In Action classes, specify the ActionForm as a parameter in the execute method or as a property to automatically receive and access form data.

11. What is the struts-default package?

In the Struts configuration, the struts-default package is a pre-defined default package. It contains all the default configurations and features that are commonly used in the Struts applications. This default package provides necessary frameworks and packages and by using them, we can build web applications quickly without having to configure everything from scratch.

This package typically include default configuration for interceptors, result types, and error handling, commonly used utility classes, and resources such as CSS files or JavaScript libraries. Developers can use these defaults as it is or can customize them according to the specific application requirements.

12. What is ForwardAction in Struts?

In Struts, the ForwardAction class is used to forward total control from one location to another within the Struts application like from a JSP to a local server.

  • It is utilized when combining an existing application with Struts.
  • Based on pre-defined configurations, it helps in directing the flow of the requests to different components.

13. How action-mapping tag is used for request forwarding in Struts configuration file?

Under action-mapping tag, forwarding options are defined in Struts configuration file i.e. struts-config.xml.

  • It used to map URLs to corresponding action classes.
  • For request processing, it specifies the pattern of URL with associated action class for same.

In the below example, if we click on the hyperlink, request will be forwarded to /pages/login.jsp using the below configuration from struts configuration file:

<action-mappings>
    <action path="/login" forward="/pages/welcome.jsp">
    </action>
 </action-mappings>

14. How can Validation Errors be displayed on a JSP page?

To display all Validation errors to user on JSP page based on the validation rules defined in validation.xml file, we can use Struts tags such as <html:errors> or <html:errors/> in JSP file. During form validation, these tags are used to display automatically generated error messages.

15. How duplicate form submission can be controlled in Struts?

To control duplicate form submission, we can use Token Interceptor provided by Struts.

  • For each form submission, it generates a unique token using saveToken() method of action class, which is then validated upon the form submission.
  • It uses isTokenValid() method to check whether the token is already used or invalid, the action will not process and it will prevent the duplicate submissions.

16. How can we access Java Beans and their properties in Struts?

To access Java Beans and their properties, we can use Bean Tag Library in Struts. This is a library which can be used to access Java beans.

17. What are the two types of Validations supported by Validator Framework in Struts?

For form data validation, Validator framework is used. The Validator framework provides two types of validation:

  • Client-side Validation: This performed in user’s web browser using JavaScript before submitting the form data to the server.
  • Server-side Validation: This validation is executed after the form data is submitted.

Struts Interview Questions for Experienced

In this section we have covered advanced topics such as Struts 2.x, Struts configuration with XML, and custom plugin development, these questions will push your knowledge to its limits and prepare you for leadership roles.

18. What is DynaActionForm?

DynaActionForm is a sub-class of ActionForm Class. It is used to dynamically create form beans. For form bean creation, it uses configuration files. It dynamically handles the form data without the need to create a separate form bean class for each HTML form.

19. What are ActionServlets and Why ActionServlet singleton in Struts?

In Struts, the ActionServlets are the central controller components and it manages the entire request handling process in the application. ActionServlet is singleton in Struts.

  • When the request received, it routed to the corresponding Action classes based on configured mappings.
  • It call to the process() method of RequestProcessor.
  • It handles the overall flow of control within the application.
  • ActionServlet is based on singleton design pattern because only one object needs to be created for this controller class and for each user’s request multiple threads are created later.

20. What is the role of the Struts configuration file (struts-config.xml)?

Struts Configuration file i.e. struts-config.xml only serves as a central configuration file for the Struts applications.

  • It maps the incoming requests URLs with appropriate Action classes.
  • Mapping is done along with the global settings such as message resources and form beans.

Refer the below sample struts-config.xml file for better understanding.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
    <!-- Define action mappings -->
    <action-mappings>
        <!-- Define mappings between URLs and action classes -->
    </action-mappings>
    
    <!-- Define global settings such as form beans and message resources -->
</struts-config>

21. What is meant by the validate() and reset() function in Struts?

  • validate(): This function is used to perform server-side validation of form inputs based on the validation rules defined in the ActionForm class.
  • reset(): This function determines whether to reset or delete form fields to their default values.

22. What are the condition for actionForm to work correctly?

ActionForm should follow the following conditions to work correctly.

  • ActionForm must have a no argument constructor.
  • For all properties, it should have public getter and setter methods.
  • It needs to extend the org.apache.struts.action.ActionForm class or it can implement the org.apache.struts.action.ActionForm interface.
  • It must be declared in the struts-config.xml file with a unique name and linked to the corresponding Action mapping.

23. What are OGNL and ValueStack in Struts?

OGNL stands for Object Graph Navigation Language. It is an expression language of Struts2. ValueStack is a stack and it contains all values and action specific object with data linked to actions. The values or data of ValueStack are changed through OGNL library.

For more details please refer to this article: Struts 2 OGNL

24. How Client side validation is enables on a JSP form?

To enable client-side validation in Struts, we need to enable the validator plug-in inside the struts configuration file i.e. struts-config.xml. By adding the below configurations in struts-config.xml file, this process can be done.

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathname"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

Now, the validation rules are defined in validation.xml file. Let’s see a simple example, if a form contains email field, and we want to do client side validation on email field, following code will be added in validation.xml file:

<form name="gfgForm">
<field property="email"
depends="required">
<arg key="gfgForm.email"/>

25. What is declarative exception handling in Struts?

  • In Struts, when the logic for exception handling has defined within the action tag or in struts-config.xml file, it is known as declarative exception handling.
  • For this, we can specify how to handle specific exceptions thrown by actions like redirecting to different actions or displaying custom error pages etc. in struts configuration file.

Conclusion

In this free interview preparation guide, we have covered a wide range of Struts interview questions and answers to help you excel in your next interview. From basic concepts to advanced topics, these questions have been carefully selected to assess your knowledge and understanding of the Struts framework.



Contact Us