Top 25 Java Struts Interview Questions You Must Prepare 19.Mar.2024

In Struts framework, actionServlet acts as a controller and all the requests made by users are controlled by this controller. ActionServlet is based onsingleton design patter as only one object needs to be created for this controller class. Multiple threads are created later for each user request.

Struts Validator Framework enables us to validate the data of both client side and server side.
There are two types of validation supported in the struts framework:-
  @Clint side validation (Using java script support and enabling  java script in ur browser).
  @Server site validation (using Validator method or ActionServlet class and).
There is another way which is defining the rules of validation in validation-rule.xml file.
When some data validation is not present in the Validator framework, then programmer can generate own validation logic, this User Defined Validation logic can be bind with Validation Framework.
Validation Framework consist of two XML configuration Files:
@Validator-Rules.xml file
@Validation.xml file

Struts framework works on the concept of centralized control approach and the whole application is controlled by a single servlet controller. Hence, we require only one servlet controller in a servlet application.

Although Struts have large number of advantages associated, it also requires bigger learning curve and also reduces trparency in the development process.
Struts also lack proper documentation and for many of its components, users are unable to get proper online resources for help.

For validation of populated properties, validate() method of ActionForm class is used before handling the control of formbean to Action class.

Controller uses a configuration file “struts-config.xml file to get all mapping information to decide which action to use for routing of user’s request.It work as the router in the network and process and map the request to the associated action or resource.

Resource files (.properties files) can be used in Struts by adding following configuration entry in struts-config.xml file:
<message-resources parameter=”com.login.struts.ApplicationResources”/>

 

The different kinds of actions in Struts are:
1.ForwardAction
2.IncludeAction
3.DispatchAction
4.LookupDispatchAction
5.SwitchAction

Exceptions are handled in struts by using any one of the following two ways:
Programmatically handling: In this exception are handled by using try and catch block in program. Using this programmer can define how to handle the situation when exception arises.
Declarative handling: In this exception handling is done by using the XML file. Programmer defines the exception handling logic in the XML file. 
There are two ways of defining the exception handling logic in the XML file:
1.Global Action Specific Exception Handler Definition.
2.Local Action Specific Exception Handler Definition.

Struts is based on model 2 MVC (Model-View-Controller) architecture. Struts controller uses the command design pattern and the action classes use the adapter design pattern. The process() method of the RequestProcessor uses the template method design pattern. Struts also implement the following J2EE design patterns.
1.Service to Worker
2.Dispatcher View
3.Composite View (Struts Tiles)
4.Front Controller
5.View Helper
6.Synchronizer Token

 

Struts framework is composed of following components:
@Based on the Model-View-Controller (MVC) design paradigm, distinctly separating all three levels:
Model: application state
View: presentation of data (JSP, HTML)
Controller: routing of the application flow
@A request handler provided by the application developer that is used to  mapped to a particular URI.
@A response handler which is used to trfer the control to another resource which will be responsible for completing the response.
@A tag library which helps developers to create the interactive form based applications with server pages.
@Java Servlets
@JSP (Java Server Pages)
@Custom Tags
@Message Resources

Struts is based on MVC and hence there is a good separation of different layers in Struts which makes Struts applications development and customization easy. Use of different configuration files makes Struts applications easily configurable. Also, Struts is open source and hence, cost effective.

Bean Tag Library is a Struts library which can be used for accessing Java be.

Action mapping contains all the deployment information for a particular Action bean. This class is to determine where the results of the Action will be sent once its processing is complete.

1.The LookupDispatchAction class is a subclass of DispatchAction.
2.The LookupDispatchAction is used to call the actual method.
3.For using LookupDispatchAction, first we should generate a subclass with a set of methods.
4.It control the forwarding of the request to the best resource in its subclass.
5.It does a reverse lookup on the resource bundle to get the key and then gets the method whose name is associated with the key into the Resource Bundle.

1.The DispatchAction enable the programmer to combine together related function or class.
2.Using Dispatch Action programmer can combine the user related action into a single UserAction. like add user, delete user and update user.
3.DispatchAction execute the action based on the parameter value it receives from the user.

In order to use Struts framework, we only need to add Struts.Jar file in our development environment. Once jar file is available in the CLASSPATH, we can use the framework and develop Strut based applications.

Yes Struts are thread safe. In Struts, a new servlet object is not required to handle each request; rather a new thread of action class object is used for each new request.

Struts support all types of models including Java be, EJB, CORBA. However, Struts doesn’t have any in-built support for any specific model and it’s the developer’s choice to opt for any model.

In Struts, action class provides two important methods which can be used to avoid duplicate form submissions.
saveToken() method of action class generates a unique token and saves it in the user’s session.isTokenValid() method is used then used to check uniqueness of tokens.

Following are the core classes provided by Struts Framework:
1.Action Class
2.ActionForm Class
3.ActionMapping Class
4.ActionForward Class
5.ActionServlet Class

 

The POJO class is light weight and easy to test with frameworks like Junit. Moreover, there is no need to create separate ActionForms as in struts one to hold the values from the source web page. It is done by a single POJO class in struts 2.

The struts.devMode is used to make sure that framework is running in development mode or production mode by setting true or false. struts.devMode is set to false in production phase to reduce impact of performance. By default it is "false". 
It is used because of the following reasons:
Resource Reloading: Resource bundle reload on every request
Modification: struts.xml can be modified without restarting or redeploying the application
Error Handling: The error occurs in the application will be reported, as oppose to production mode.

Struts should be used when any or some of the following conditions are true:
1.A highly robust enterprise level application development is required.
2.A reusable, highly configurable application is required.
3.A loosely coupled, MVC based application is required with clear segregation of different layers.