This Tutorial on Struts Interview Questions with Answers Explains Struts Concepts With Simple Examples:
This series of Struts questions will take you through the complete overview of Struts Framework, Flow of an application, need for Struts, its actions, constants, etc., along with the inputs used in Struts. This article will cover all the important questions that are asked in any Struts interview.
Nobody will ask you to write code with Strtus2, it will hover around all the theoretical questions and the interviewer just wants to know whether the candidate has basic knowledge on the flow of the Struts framework or not.
After going through all these questions thoroughly, if you can explain the concepts in your own words with small real-time examples, you are through then.
Table of Contents:
Most Frequently Asked Struts Interview Questions With Answers
Q #1) What is Struts Framework? What are its Components?
Answer: Struts is a type of Web Application Development Framework that provides a suitable platform for developing web applications. Struts include Servlets, JSP, Custom tags and Message resources into a unified framework.
Q #2) Why do we use Struts?
Answer: As Struts is based on MVC architecture i.e. Model, View, Controller, it separates Business Logic, Design & Controller, as a result of which it makes the code more simple, maintainable and readable.
Q #3) What are the important classes of the Struts Application?
Answer: Enlisted below are the most important classes of the Struts Application.
- Action Servlet: It is the controller class and handles all the incoming requests.
- Action Class: With Action class all the business logic also so-called a Model is developed.
- Action Form: Action Form is a java bean that associates one or more Action Mappings. It maintains the session state for a web application.
- Action Mapping: With Action Mapping, we can do the mapping between Object and Action.
- Action Forward: It is used to forward the result from the Controller to the destination.
Q #4) What are the new features of Struts2?
Answer: The prominent features of Struts2 are as follows.
- In Struts2, the Action class is POJO. We don’t need to inherit any class or implement any interface.
- Struts2 has JSP, Free marker, and Valocity for the view component.
- In Struts2, Front Controller is StrutsPrepareAndExecuteFilter.
- In Struts2, the configuration file name must be struts.xml and should be placed inside the classes directory.
- Struts2 uses the concept of Interceptors while processing the request.
Q #5) What is MVC?
Answer: MVC stands for Model, View, and Controller and is called the Software Design Pattern for developing web applications.
Model: Model is responsible for maintaining the State of Data and Business logic of the application.
View: View is responsible to show all data to use i.e. it shows the presentation.
Controller: Controller acts as an interface between View and Model. It intercepts all the requests i.e. receives input and commands to Model/View to change accordingly.
Q #6) What are Interceptors?
Answer: Interceptors are the object that provides the pre-processing logic before action is called or provides a post-processing logic after the action is called. Interceptors can be used to perform various operations such as Validations, Exception Handling, File Uploading, Displaying intermediate results, etc.
Q #7) What are the Life Cycle methods of the Interceptor?
Answer: There are three life cycle methods of the Interceptor as mentioned below.
- init () method
- intercept () method
- destroy () method
Q #8) How is a request handled in Struts2 Application?
Answer: A request in Struts2 Application is handled in the below manner.
- Firstly, the request received is sent to the server by the user.
- Then the request is handled by FilterDispatcher that selects an appropriate action for it.
- The defined interceptors perform pre-processing operations like file upload, exception handling, etc.
- Now the selected action is executed and the operation is performed.
- Again the defined interceptors perform post-processing operations like file upload, exception handling, etc.
- The final result is displayed to the user in the form of the view page.
Q #9) What is the function of the Struts.xml File in Struts?
Answer: In the struts.xml file, the user can define all the mapping to actions so that a particular action is called when a particular operation is performed. It is also called as the configuration file and is present under the WEB-INF/classes folder.
Q #10) Which Configurations Files are used in Struts?
Answer: The configurations files used in Struts include:
- ApplicationResource.properties.
- Struts-config.xml.
Q #11) What is the use of Validate () and Reset () methods in Struts?
Answer:
Validate () method is used to validate the properties after they have been populated and it is called before the Form Bean is passed to the action. It returns a collection of action errors.
Syntax: Public ActionErrors Validate (ActionMapping mapping, HttpServletRequest request)
Reset () method is used to reset all the ActionForm’s data before the new values are being set. It is called by the struts framework with each request that uses a defined action form.
Syntax: public void reset ((ActionMapping mapping, HttpServletRequest request)
Q #12) What do you mean by Value Stack?
Answer: A Value Stack can be explained as a stack that contains requirement specific objects. The action stays at the top of the stack while executing.
The objects are then placed in Value Stack and the user can perform SQL operations on it.
The various methods of Value Stack are as follows:
- FindString (): Used to find the string from the given expression.
- FindValue (): Used to find value from the given expression.
- Peek (): It gives the object located at the top of the stack.
- Pop (): It gives the object located at the top of the stack and removes it.
- Push (): It pushes the object to the top of the stack.
Q #13) What do you mean by Action Context?
Answer: The Action Context is an object container in which actions are executed. The values that are stored in the Action Context are unique per thread (i.e. Thread Local), as a result of which, we don’t need to make our action thread-safe.
By calling the getContext() method of ActionContext class, we can get the reference of ActionContext. It is a static factory method.
For Example: ActionContext context = ActionContext.getContext();
Q #14) What is the use of ActionInvocation?
Answer: The ActionInvocation is used to represent the action’s execution state. It holds the action and the interceptor object.
Q #15) What is the use of OGNL?
Answer: Object Graph Navigation Language (OGNL) is a strong expression language. It helps to simplify the accessibility of data stored in the ActionContext. OGNL supports interacting with collections i.e. Map, List, and Set.
The struts framework helps to set the ValueStack as the root object of OGNL. The action object is pushed into the ValueStack and we can directly access the action property.
Q #16) What are the types of constants present in the Action Interface?
Answer: There are five types of constants provided by the Action Interface.
They are:
- Login
- Input
- Success
- Error
- None
Q #17) How can we create Action Classes in Struts2?
Answer: There are many ways to create Action Classes in Struts2.
Some of them are as follows:
- Using Strts2 @Action annotation.
- Extend the ActionSupport class.
- By implementing the Action Interface.
- Any java class consisting of execute () returning value can be configured as the Action Class.
Q #18) How can we Handle Exceptions in Struts?
Answer: In Struts, exceptions can be handled in two ways as shown below.
- Programmatic Exception Handling: In this, we can use Try {} Catch {} block to handle the exception.
- Declarative Exception Handling: Here, we can define the global exception handling tags in struts-config.xml or define exception handling tags within tags.
Q #19) What are the Types of Tag Libraries in Struts?
Answer: Struts provides many tag libraries to ease software development.
They are:
- Bean Tag Library: Used for accessing Java Beans and its properties.
- Nested Tag Library: Provides the ability to use nested beans in the application.
- Logic Tag Library: Used for giving the required output, iteration capability and flow management.
- HTML Tag Library: Helps to get the required HTML output.
- Tiles Tag library: Used in applications that have tiles.
Q #20) What are the different types of Struts Actions?
Answer: There are 5 types of Struts Actions.
They are as follows:
- Forward Action: With this class, we can control the Struts controller and its functionality, without having to rewrite the existing Servlets.
- Include Action: Using IncludeAction class to include another resource in the response to the request being processed.
- Switch Action: Used to switch one resource in one module to another resource in another module.
- Dispatch Action: Used to combine the related actions into a single class.
- LookUpAction: Used to drive the action with a key if the action name is not working.
Q #21) What are the different types of tags in Struts2?
Answer: The various types of tags in Struts2 are as follows:
- Control Tag (If Else tag, Iterator Tag, Merge Tag, Append Tag, Generator Tag, etc.)
- Data Tag (Action tag, Include Tag, Bean Tag, Date Tag, Param Tag, Property Tag, Push Tag, Set Tag, Text Tag, etc.)
- Form Tag (All the simple UI Tags.)
- Ajax Tag
Q #22) What is the default result type in Struts2?
Answer: The default result type in struts2 is the dispatcher. It is used to dispatch the JSP pages.
Q #23) What do you mean by Internationalization? Why do we need it?
Answer: Internationalization (i18n) is the interceptor that is used for planning and implementing products and services so that they can be easily adapted to specific local languages and cultures i.e. a process called localization.
It handles setting locale for the action. It can be used if the user wants to set his/her locale and get data according to the locale provided.
Q #24) What are the Core Components of Struts2?
Answer: The core components of Struts2 are as follows:
- Interceptors
- Value Stack
- Action Context
- OGNL
- ActionInvocation
Q #25) How can we create Custom Interceptor?
Answer: We can create the custom interceptor by implementing the Interceptor interface in a class and overriding its three life cycle method i.e. init (), Destroy (), Intercept (). It can also be created by defining the entry of the interceptor in the struts.xml file.
Q #26) What is the functionality of the Token Interceptor in Struts2?
Answer: The major problem with web applications is the double form submission that results in charging double amounts from the user and storing double values in the database. We can use a token interceptor to solve the double form submission problem. This interceptor is defined in the struts-default package.
Q #27) How to upload files in the Struts2 Application?
Answer: With the help of the FileUploadInterceptor, we can upload files in the struts2 application.
Q #28) Is Struts2 Action Synchronized?
Answer: Yes, Struts2 Actions are synchronized as an object is invoked to handle any new incoming request.
Q #29) Are Struts2 Interceptors Synchronized?
Answer: As Struts2 interceptors are singleton classes and as a new thread is created to handle the request, it is not synchronized and we need to implement them carefully to avoid any issues with the shared data.
Q #30) What is the Front Controller in Struts2?
Answer: StrutsPrepareAndExecuteFilter is the Front Controller class in Struts2, it is also the starting point of every incoming request.
Q #31) How are ValueStack and OGNL linked?
Answer: OGNL is a library through which the ValueStack data or values are manipulated whereas ValueStack is a stack where all values and data related to actions are stored.
Q #32) Write the syntax to display the output page using XML in Struts2?
Answer: Here in Struts2, a user can declare the XML file as shown below:
<actionname="output “class="com.softwaretestinghelp.actionname"> <result name="SUCCESS">output.jsp</result> <result name="ERROR">error.jsp</result>
Q #33) Which Design Pattern is followed by the Interceptors?
Answer: Interceptors follow a design pattern based on the intercepting filters.
Q #34) Where is the Plug-in tag found in Struts2? What is the function of the Plug-in tag?
Answer: Plug-in tag is found in the struct-config.xml and is used to tell the user where the properties file that has all the prompt and error message is found.
Q #35) What are the disadvantages of the Struts Framework?
Answer: Some of the disadvantages of the Struts Framework are as follows:
- Only one servlet controller is used.
- No proper documentation.
- Transparency is not full.
- The approach is inflexible.
- A lot of things are there to learn.
Conclusion
We have learned a lot about the Struts Framework from this informative article. We saw what Struts Framework is? The flow of Struts Framework, need for Struts, advantages of Struts over other frameworks, Struts Architecture & Components, Classes, and its Configuration files.
We also explored the concept of Interceptors, Action Context, OGNL, Action Invocation, Life Cycle methods, Different types of Tags, Exception handling in Struts, etc.
Hope this article helped you in understanding the Struts concepts easily. You can confidently answer questions on Struts with the help of this informative article.