Struts Interview Questions and Answers

Thumb

Author

BIQ

Questions

25

Last updated

Feb 2, 2022

Most Frequently Asked Struts Interview Questions

Here in this article, we will be listing frequently asked Struts Interview Questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q1. What is Struts? Explain
Answer

Struts is a free, MVC-based, open-source framework that helps developers create modern Java applications. This framework prefers convention over configuration and is extensible through a plugin architecture.

Struts was initially built by Craig McClanahan. The first stable version was released in June 2001. Struts 2.0 was released in 2014.

Q2. Explain the components of Struts?
Answer

Following are the key components of Struts:

  • JSP Programs or View Layer Resources
  • FormBean Class Java Class or Controller Layer Resources
  • Action Servlet Built-in Controller Servlet or Controller Layer Resources
  • Action Class Java Class or Controller Layer Resources
  • web.xml Deployment Descriptor file of the web application
  • Struts Configuration File XML File or Controller Layer Resources
Q3. What is the difference between struts and spring? Explain
Answer
S.no Spring MVC Struts
1. Noninvasive Invasive
2. Loosely coupled Tightly Coupled
3. Layered architecture Not layered architecture
Q4. What is the differences between Struts1 and Struts2?
Answer
S.no Struts 1 Struts 2
1. Form beans define properties, setters and getters Getters and Setters are defined in action classes
2. Execute() method exposes servlet API for testing Dependency Injection simplifies the testing process
3. Multiple tag libraries The single library includes all tags
4. RequestProcessor class present Interceptors present
Q5. How we can Install Struts?
Answer

Installation process can be divided in four steps that's are given below:

  • Setup Java Development Kit
  • Setup Apache Tomcat
  • Setup Eclipse
  • Setup Struts2 Libraries
Q6. Explain the difference between DispatchAction and LookupDispatchAction in Struts?
Answer
S.no DispatchAction LookupDispatchAction
1. Parent class of LookupDispatchAction Subclass of Dispatch Action
2. More useful if not using Internalization function Useful when using Internalization
3. Not useful in I18N Useful in I18N
Q7. How we can controlled duplicate form submission in Struts?
Answer

Duplicate form submission can occur when you by refresh the page, press back button, or if there is a virus in your machine.

There are 2 ways to prevent duplicate requests:

  • Through isTokenValid() method
  • By managing clearing request object.
Q8. What do you mean by ActionServlet?
Answer

ActionServlet is like a "controller" in the MVC architecture for web apps which is known as Model 2. The requests to server pass through the controller, which is responsible for managing requests.

The usual flow in struts begins with the ActionServlet and call to process() method of RequestProcessor.

Q9. What do you mean by ActionForm?
Answer

An ActionForm is a JavaBean that is associated with ActionMappings and handles session state for the apps. The ActionForm object populates automatically on the server side when any set of data is entered on the client side.

If you are preparing for Struts interviews, you can read Struts interview questions and answers for freshers for clarity and insight.

Q10. What is validate() and reset() functions?
Answer

validate() is like a hook that can be overridden by different subclasses for performing validations on incoming data. This method gets called after a Form Bean gets populated with incoming data. This method includes return type ActionErrors.

Syntax is:

public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)

reset() is a method that gets overridden by subclasses. It is a hook that gets summoned before FormBean gets populated with request data from HTML. Using this method, all form fields get reset.

Syntax is:

public void reset() {}

Q11. What do you mean by ActionMapping?
Answer

ActionMapping represents the information that is known to RequestProcessor about the mapping of a specific request to a specific Action class. This instance is used to select an Action and then passing on to that Action, thus providing access to configuration information included in the ActionMapping object.

Q12. Explain design patterns which is used in Struts?
Answer

Struts use the model 2 MVC architecture. The controller takes the command design pattern and the action classes have the adapter design pattern. In addition, the process() method of RequestProcessor uses template method design pattern.

This framework also implements the J2EE design patterns:

  • Service to Worker
  • Dispatcher View
  • Composite View (Struts Tiles)
  • Front Controller
  • View Helper
  • Synchronizer Token
Q13. What is the configuration files used in Struts?
Answer

The Struts framework uses one or more configuration files to load and create required application-specific components. These configuration files allow the components to be declaratively specified, rather than having hardcoded behavior and information. This can give you the flexibility to provide your own extensions. Based on XML format, configuration files can be validated against Struts DTD.

 

We can configure with the help of few important configuration files like struts.xml, web.xml,strutsconfig.xml and struts.properties

Q14. What is the difference between validation.xml and validator-rules.xml files in Struts?
Answer
S.no Validator-rules.xml Validation.xml
1. Defines standard validation routines Defines validations that apply to form bean
2. For global specific rules For application specific rules
3. Contain logic for validation Contains types of validation
Q15. How can we display validation errors on JSP page?
Answer

Validation error arises when a user enters invalid data format into the form. In order to validate this data, Struts enables the developer or programmer with the Validator() method for validating the data from the client side as well as the server side. You can display errors in the JSP page by using this syntax:
SYNTAX: <html:error/>

Q16. What do you mean by DynaActionForm?
Answer

In Struts, DynaActionForm is a feature which allows the dynamic creation of ActionForm beans through the XML configuration. In such applications, Form beans are virtual and not hard-coded ActionForm classes.

This reduces the tedious process of managing and creating ActionForm classes.

Q17. What do you mean by tiles in Struts?
Answer

Tiles are used for creating reusable presentation components. When using Tiles, first define a base layout with various sections. Next, define jsp page that should fill the corresponding regions in an external configuration file.

 

There are two ways to specify definition and attributes of Tiles - JSP Tile Definition and XML Tile Definition.

Q18. What do you mean by OGNL?
Answer

OGNL or Object-Graph Navigation Language is an open-source Expression Language that allows the navigation of objects (through syntax) for specifying symmetrically settable values. The syntax provides a high level of abstraction for navigating object graphs. This feature allows for simple array manipulation.

Q19. Explain the difference between Jakarta Struts and Apache Struts?
Answer

Technically, there is no difference between them Apache Struts and Jakarta Struts. They are both similar. Craig McClanahan created Struts and donated it to Apache Foundation. Later, it evolved as a Jakarta project and gained popularity in the J2EE community.

Q20. What are the Core classes of Struts Framework? Explain
Answer

There are 5 core classes in Struts framework:

  • ActionServlet
  • ActionForm
  • Action
  • Action Mapping
  • ActionForward

 

This is one of the most repeatedly asked Java Struts 1.2 interview questions and answers.

Q21. How we can configured action mapping in Struts?
Answer

To configure the Struts2 action mapping we have to add an action node inside the configuration file struts.xml.

Here is an example for this:

<action name="*welcome" class="example.action.mapping.LinkAction" method="execute">

     <result name="success">/welcome.jsp</result>

     <result name="error">/error.jsp</result>

</action>

Q22. Which technologies can be used at View Layer in Struts?
Answer

You can use the following technologies in the view layer in Struts:

  • JSP
  • HTML
  • XML/XSLT
  • WML Files
  • Velocity Templates
  • Servlets
Q23. Lists the bundled validators in Struts?
Answer

Struts framework offers a number of built-in validators which are called as bundled validators. These validators are for input validations such as email, phone number, user id.

Struts offer the following bundled validators.

  • requiredstring validator
  • stringlength validator
  • email validator
  • date validator
  • int validator
  • double validator
  • url validator
  • regex validator
Q24. What is the use of jsonValidation in struts?
Answer

jsonValidation interceptor is utilized to perform AJAX validation.

Q25. Explain the difference between plain-validator and field-validator in Struts?
Answer

Field validators

  • Act on single fields that are accessible through action
  • More generic
  • Offer validations in full content

Non-field validators

  • Only add action level messages
  • Mostly domain specific
  • Offer custom implementations
  • Allows to declare both types of validators