Struts is one of the well-known Java frameworks that has been instrumental in developing a wide range of applications across the IT industry. Developed by Apache, Struts is an open-source framework that provides a robust implementation of the MVC (Model-View-Controller) architecture. It simplifies the development process by offering ready-made MVC classes that developers can use and extend, along with a built-in validation mechanism. The model layer of Struts stands out for its ability to integrate seamlessly with other Java technologies such as JDBC, EJB, Spring, and Hibernate. While the original version of Struts is less common in today's industry, most businesses now prefer Struts 2, which introduces enhanced functionalities and improved features, making it a more powerful framework for modern Java applications.
Let's discuss the top Struts interview questions with their detailed solutions.
Struts Interview Questions for Freshers
1. What do you mean by Struts?
Java Enterprise Edition Web applications may be created fast and easily using the open-source Struts Framework. Utilizing and extending the Java Servlet API, it promotes the model-view-controller (MVC) architecture.
2. What characteristics does Struts have?
Setting up MVC components
Action based on POJO
Support for AJAX
Different Tag Support Different Result Types
Integration Assistance
3. Describe interceptor.
Interceptor is a pluggable object invoked during a request's pre and post-processing.
4. What are the interceptor's life cycle methods?
public void init ()
public void intercept(ActionInvocation aIn)
public void destroy ()
5. Describe MVC.
A design pattern is MVC. MVC Stands for Model, View, and Controller. A controller serves as an interface between a model, which represents data, and a view, which provides a presentation.
6. Describe the various Struts framework elements.
The Struts framework is made up of the following elements:
FormBean Class (Controller Layer Resources)
JSP Programs (View Layer Resources)
Action Class
Action Servlet(Built-in Controller Servlet)
Struts Configuration File (XML File)
web.xml (Deployment Descriptor file of the web application)
7. What are the main Struts application classes?
The crucial classes for any application of Struts are listed below.
Action Servlet
Action Form
Action Class
Action Forward
Action Mapping
8. What should we do initially to use the Struts application?
We just need to add the Struts.jar file to our development environment to use the framework. We can use the framework and create Strut-based apps once the jar file is in the CLASSPATH.
9. What is the OGNL?
A Struts2 expression language is OGNL. OGNL stands for Object Graph Navigation Language.
The execAndWait interceptor, also called ExecuteAndWait, is used to show interim or wait results.
11. What advantages does the Struts framework offer?
Because Struts is built on MVC, there is a strong separation of the various layers, making the creation and modification of Struts applications simple. Struts applications are easily changeable thanks to the use of several configuration files. Struts is also open source and therefore inexpensive.
12. How does Struts 2 respond to the request?
The user first communicates with the server after receiving the request.
After that, FilterDispatcher manages the request and decides what to do with it.
The specified interceptors carry out pre-processing tasks, including file upload, error handling, etc.
The chosen action is now carried out, and the operation is finished.
Interceptors are used to carry out post-processing tasks like file upload, exception handling, etc.
The user gets shown the finished product in the form of a view page.
13. Name a few drawbacks of the Struts framework.
Nothing is documented.
It isn't entirely transparent.
Only one servlet controller is there.
There is so much to learn.
The approach is unwavering.
14. Why do we use ActionServlet?
In the Model 2 MVC architecture for online apps, ActionServlet functions similarly to a "controller." The controller, which is in charge of controlling requests, is where the requests to the server are sent.
The ActionServlet and the call to the RequestProcessor's process() method start the standard flow in struts.
15. What is ActionForm?
A JavaBean called an ActionForm manages the apps' session state and is connected to ActionMappings. Any time a data set is entered on the client side, the ActionForm object immediately populates itself on the server.
16. In Struts, what are OGNL and ValueStack mean?
ValueStack is a stack where all data and values associated with actions are saved, whereas OGNL is a library where the data or values in ValueStack are updated.
17. How should exceptions be handled in Struts2?
The mechanism for handling exceptions in Struts2 is extremely thorough. After packages have defined global outcomes, specific exceptions can be mapped to these result pages. Both the global package and action levels can do exception mapping.
18. Why is ActionServlet a singleton in Struts?
The Struts framework's ActionServlet controller handles all user requests. ActionServlet is based on the singleton design pattern since only one object needs to be constructed for this controller class. Many threads are created in response to each user request.
19. Describe how Struts 2's token interceptor works.
The most frequent problem with online applications is a double form submission, which causes the user to be charged twice and the database to store double values. We can use a token interceptor to stop multiple form submissions. This interceptor is defined in the struts-default package.
20. What does package struts-default stand for?
All of the Struts2 interceptors are defined in the struts-default package, along with the most popular interceptor stack. Extending this package while configuring our application package is advisable to avoid having to configure interceptors twice. This information is provided to developers to assist them in setting up interceptors and result pages for our application.
Let us now discuss the Struts Interview Questions for Experienced.
Struts Interview Questions for Experienced
21. Explain the differences between basic HTML tags and HTML tags specific to Struts.
The key distinctions between standard HTML tags and HTML tags specific to Strut are as follows:
HTML tags are static by nature, whereas HTML tags specific to Struts are dynamic.
While Struts coordinates HTML, it also has its own tag libraries.
The tags for struts, however, can be user-defined, unlike the tags for HTML.
22. What occurs when an HTTP request is sent from a client browser?
After a request is sent, the subsequent actions occur:
The ActionServlet receives the request.
The struts-config.xml file contains details about Actions, ActionForms, ActionMappings, and ActionForwards.
When the ActionServelet starts up, it reads the struts-config.xml file and creates a database of configuration objects. The ActionServlet later uses this object to make a decision when handling the request.
23. On a JSP form, how is client-side validation enabled?
Before enabling client-side validation in Struts, we must first enable the validator plug-in in the struts-config.xml file. The following configuration elements are added to this file:
The validation.xml file then contains the rules for validating data. The following code is added to the validation.xml file if a form contains an email field and if we want to enable client-side validation for this field:
24. What does the action class' Execute method serve?
The action class's Execute method is in charge of carrying out business logic. This approach is used whenever processing the user's request is necessary. The ActionForward object returned by this method directs the program to the proper page.
The execute method will return an object of the type ActionForward with the name "exampleFunAction" defined in the struts-config.xml example below:
25. How is the action-mapping tag used in the Struts configuration file for request forwarding?
The action-mapping element in the Struts configuration file (struts-config.xml) defines the forwarding options.
When a user clicks on the hyperlink test.do in the example below, the request will be routed to /pages/testing.jsp using the following configurations from the struts-config.xml file:
27. What makes Struts1 and Struts2 distinct from one another?
Struts 1
Struts 2
ActionForms are a feature of Struts 1. These classes match the JSP forms. The Action classes utilize these ActionForms' data to fill Data Transfer Objects.
No action forms are in use. No DTO is necessary because the JSP form translates straight to POJO classes. Because fewer classes are needed, less maintenance is required.
The language of expression is JSTL (Java Standard Tag Library).
Additionally, employed as an expressive language, OGNL (Object Graphic Notation Language) is more potent than JSTL. JSTL can, however, also be utilized with struts 2.x.
It has several tag libraries.
Numerous tags are present in a single library.
Action classes are limited in their ability to extend other classes since they must extend the abstract base class.
The execute() method on a POJO class can be used to create more versatile Action classes. Action classes can also be created by extending the ActionSupport class or implementing the Action interface.
HttpServletRequest and HttpServletResponse parameters of type HttpServletRequest and HttpServletResponse are passed as arguments to action classes' execute function. Because of this, struts 1.x actions depend on the servlet API.
The Action class in Struts 2 does not depend on a servlet because the execute() function does not accept any parameters. The request and answer are still accessible to Struts 2 Actions, though.
28. What separates field validators from plain validators?
Validators at the action level can use the plain-validators syntax. Here, many fields can all be validated by a single validator. The disadvantage of this method is that we can only utilize a limited number of validators on a single field.
Field-validator syntax applies to the field-level validator. In this situation, one field may use many validators. The disadvantage of this method, as opposed to plain-validator, is that only one validator can be used across several fields.
29. What do the validate() and reset() functions mean?
The validate() method performs validations on incoming data, which other subclasses can override. This function is called once an incoming Form Bean has been filled with data. This method includes a return type for ActionErrors.
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {}
The reset() can be overridden by subclasses. It is a hook that is activated before the FormBean receives the HTML request data. This method resets all form fields.
public void reset() {}
30. Describe a few of the Struts2 annotations that are useful.
Important annotations included in Struts2 include
1. Creating an action class with @Action.
2. Using @Actions, you can set up a single class for numerous actions.
3. Utilizing @Namespace and @Namespaces to create various modules.
4. Result pages with @Result.
5. @ResultPath for setting the placement of result pages.
31. How does the ActionMapping class work in Struts?
In Struts, the ActionMapping class plays a crucial role in associating an incoming request with the appropriate Action class. It maps a specific URL to a corresponding Action class, enabling the framework to delegate the processing of requests to the right components. ActionMapping helps in configuring the relationships between the request path and the business logic. It contains information about the path of the action, form bean, validation rules, and the forwards (views) to which control is sent after processing.
32. What is the role of the RequestProcessor in Struts?
The RequestProcessor class in Struts controls the entire lifecycle of the request. It’s responsible for receiving incoming requests and directing them to the appropriate Action class. The RequestProcessor performs the following tasks:
Decodes the request URL to find the relevant action.
Validates the input data.
Calls the Action class's execute() method to process the business logic.
Decides the next view to forward to, based on the action's result. The RequestProcessor is customizable, allowing developers to extend it to implement pre-processing and post-processing tasks.
33. How do Struts and Spring frameworks integrate with each other?
Struts and Spring integration is common in Java enterprise applications where Struts handles the web layer and Spring takes care of the business logic. The integration is seamless because Struts 2 provides a plugin architecture. To integrate:
The Action classes in Struts can be injected with Spring beans, utilizing Spring's dependency injection.
Use the Struts-Spring plugin, which enables the Struts actions to be managed as Spring beans.
Spring beans are defined in the applicationContext.xml, and the Struts configuration files (like struts.xml) can refer to these beans. This integration provides flexibility, easier maintenance, and separation of concerns.
34. What are the key differences between Struts 1.x and Struts 2.x?
Struts 1.x and 2.x are significantly different frameworks, though they share a similar architecture. Key differences include:
Action Classes: Struts 1.x requires action classes to extend Action, while Struts 2.x allows actions to be simple POJOs.
FormBeans: Struts 1.x uses ActionForm beans, whereas Struts 2.x eliminates the need for FormBeans by allowing the use of any JavaBeans.
Interceptors: Struts 2.x introduces interceptors that provide cleaner pre-processing and post-processing logic, unlike Struts 1.x.
Tag Libraries: Struts 2.x has a more flexible tag library, and it also supports OGNL (Object-Graph Navigation Language) for binding data directly to the page.
35. How can you handle file uploads in Struts 2?
Struts 2 provides a simple mechanism to handle file uploads via its built-in FileUploadInterceptor. The steps to handle file uploads are:
Configure the FileUploadInterceptor in the struts.xml file.
In the action class, declare attributes to hold the uploaded file, content type, and file name.
Use the getters and setters to process the uploaded file. For example:
4. Ensure the form in the JSP file uses enctype="multipart/form-data" to allow file uploads. The framework handles the file's temporary storage, and you can specify file size limits and allowed file types through interceptor configuration.
36. Explain the role of ValueStack in Struts 2.
ValueStack is one of the core components of Struts 2, responsible for storing all the data associated with the request. It holds values such as action properties, temporary variables, and form data. When an action is invoked, its properties and values are automatically pushed onto the ValueStack. The OGNL (Object Graph Navigation Language) is used to access and manipulate these values from the JSP or other view technologies. ValueStack ensures a clean separation between business logic and presentation layer, allowing easy access to data without needing complex code in the view.
37. How can you implement validation in Struts 2?
Validation in Struts 2 can be implemented using either XML-based validation or annotation-based validation.
XML-based Validation: Developers can create an XML file corresponding to the action class (for example, MyAction-validation.xml), where validation rules are defined. The framework automatically invokes these rules during form submissions.
Annotation-based Validation: Struts 2 supports Java annotations for validation. By using annotations like @RequiredStringValidator, @EmailValidator, or @IntRangeFieldValidator directly in the action class, developers can enforce validation logic. Validation ensures that form input is properly checked before business logic is executed, improving the security and robustness of the application.
38. What is the significance of Interceptors in Struts 2?
Interceptors in Struts 2 are a powerful mechanism that allows developers to define reusable pre-processing and post-processing logic for actions. Interceptors are executed before and after the action invocation, providing functionalities such as:
Input Validation: Validate user input before the action executes.
Authentication: Check if the user is authenticated.
File Upload Handling: Process file uploads.
Logging: Track actions and execution time. Interceptors are configurable via the struts.xml file and can be applied globally or per action.
39. How can you configure global exception handling in Struts 2?
Global exception handling in Struts 2 can be configured in the struts.xml file by defining global exception mappings within the package tag. This allows developers to map specific exceptions to result pages. For example:
In this example, any exception thrown within the application will redirect to the errorPage result, ensuring a consistent handling strategy across the application.
40. Explain how Struts 2 integrates with AJAX.
Struts 2 provides built-in support for AJAX through its dojo plugin and the json result type. By leveraging these features, developers can create AJAX-based interactions without page reloads. Here's a simple way to integrate AJAX in Struts 2:
Use the json result type to return data in JSON format from an action.
Use JavaScript (or libraries like jQuery) on the client side to make asynchronous calls to the server.
On receiving the JSON response, dynamically update the parts of the webpage without a full reload. Struts 2 makes it easy to develop modern web applications with interactive UI elements.
Struts Framework MCQ
41. What does Struts framework use for client-side validation?
A. JavaScript
B. AJAX
C. Struts Validator
D. OGNL
Answer: C. Struts Validator
42. Which configuration file is essential for Struts?
A. web.xml
B. struts-config.xml
C. applicationContext.xml
D. faces-config.xml
Answer: B. struts-config.xml
43. What pattern is the Struts framework based on?
A. Factory pattern
B. Singleton pattern
C. Model-View-Controller (MVC)
D. Observer pattern
Answer: C. Model-View-Controller (MVC)
44. In Struts, which tag library is used to manage forms?
A. HTML Tag Library
B. Logic Tag Library
C. Bean Tag Library
D. Struts Tag Library
Answer: D. Struts Tag Library
45. Which of the following methods in Action class must be overridden in Struts?
A. doGet
B. doPost
C. execute
D. service
Answer: C. execute
46. What does the <action> tag in struts-config.xml file represent?
A. Mapping of URL to action class
B. Forwarding a request to another resource
C. A validation rule
D. A JSP page reference
Answer: A. Mapping of URL to action class
47. How are the form beans defined in Struts?
A. In the web.xml file
B. In the applicationContext.xml file
C. In the struts-config.xml file
D. In the JSP page
Answer: C. In the struts-config.xml file
48. What is the default scope of ActionForm beans in Struts?
A. request
B. session
C. application
D. none
Answer: A. request
49. Which interceptor is used in Struts to handle file uploads?
A. FileInterceptor
B. FileUploadInterceptor
C. UploadHandler
D. MultipartInterceptor
Answer: B. FileUploadInterceptor
50. What is the purpose of ActionMessages in Struts?
A. To store form input data
B. To send validation errors to the user
C. To manage session attributes
D. To handle file uploads
Answer: B. To send validation errors to the user
Conclusion
This article covers some of the most frequently asked Struts interview questions to help you prepare for technical interviews. If you found these Struts interview questions helpful, be sure to explore our other blogs for more insightful content.