Struts 2 Interview Questions and Answers

What is MVC?

MVC stands for Model-View-Controller design pattern which separates the business logic, presentation logic and navigation logic.
Where:
Model: is responsible for encapsulating the application data (POJO).
View: is responsible for rendering the model data.
Controller: is responsible for receiving the user request, building model object and passing the model object to the view.

What is Struts framework?

Struts is an elegant, extensible, open source framework for developing java web application. It is used to develop Model-View-Controller (MVC) architecture based web applications.
Struts 2 was originally known as webwork 2. Later on webwork and struts combined to produce a new version of struts i.e. struts 2 framework is the combination of webwork and struts.
Struts 2 = webwork + struts 1.

See more at: Struts framework

What is the difference between struts1 and struts2?

Struts 1 Struts 2
  1. Struts 1 Action classes must extends an abstract base class.
  2. Struts 1 Actions are thread safe because they are singletons.
  3. Struts 1 Actions are depends on the servlet API because the HttpServletRequest and HttpServletResponse have to be passed in the execute method.
  4. Struts 1 use the ActionForm’s object to receive the form input.
  5. Struts 1 use the JSTL expression language.
  6. Struts 1 uses the standard JSP mechanism for binding objects into the page context.
  7. Struts 1 provides less flexible type conversion.
  8. Struts 1 uses manual validation by validate method on the ActionForm.
  1. Struts 2 Action classes not have to extends any abstract class. They may implement an Action interface.
  2. Struts 2 Actions are not thread safe.
  3. Struts 2 Actions are not depend on the servlet API.
  4. Struts 2 eliminate the need of ActionForm.
  5. Struts 2 use the “Object Graph Notation Language” (OGNL) expression language.
  6. Struts 2 uses the ValueStack technology for binding values into views.
  7. Struts 2 provides more flexible type conversion.
  8. Struts 2 allows manual validation by using the validate method. It also the XWork Validation framework.

Explain struts 2 request life cycle.

1. When a request comes web container maps the request in the web.xml and calls the controller (FilterDispatcher).
2. FilterDispatcher calls the ActionMapper to find an Action to be invoked.
3. FilterDispatcher calls the ActionProxy.
4. ActionProxy reads the information of action and interceptor stack from configuration file using configuration manager (struts.xml) and invoke the ActionInvocation.
5. ActionInvocation calls the all interceptors one by one and then invoke the action to generate the result.
6. Action is executed ActionInvocation again calls the all interceptors in reverse order.
7. Control is returned to the FilterDispatcher.
8. Result is sent to the client.

See more at: Struts 2 request life cycle.

What are the core components of a Struct2 based application?

• Actions
• Interceptors
• Value Stack / OGNL
• Results / Result types
• View technologies

Explain Struts 2 Configuration File.

Struts.xml file:
The struts.xml is the core configuration file for struts framework. The struts.xml is used to initialize the resources like Interceptors, Action classes and Results.
See more at: Struts Configuration File.

How to create Action in struts 2?

In struts 2 an action can be created by following ways:

Action in struts 2 by using POJO class with example.
Action in struts 2 by implementing Action interface with example.
Action in struts 2 by extending ActionSupport class with example.

How to use multiple struts config file in struts2?

We can break struts.xml file into multiple small files. At runtime there can be only one struts.xml for an application so other files have to be included in the default struts.xml file.

<struts>
       //Other attributes
      <include file="strutsfile1.xml"/>
      <include file="strutsfile2.xml"/>
      //Other attributes
</struts>

See more at: Multiple configuration file in Struts 2.

Explain OGNL in struts 2?

OGNL refers to Object Graph Navigation Language. It is mainly used to set or get the value of a java bean property.
Use of OGNL in struts 2.
1. As an expression language it is used in forms and response UI pages.
2. It is used to access the objects in ValueStack.
3. It is used in type conversion. When a form is submitted form values are transmitted as string values. OGNL converts these values to the specific types at the time of setting these values to the java beans.

See more at: OGNL in struts 2.

Explain Value Stack in struts 2.

In struts 2 ValueStack represents a stack that contains the references of application specific objects. When a request come an object of ValueStack is created first and after it object of Action, Model or any other java resources are created. The references of all these objects are maintained in ValueStack.

See more at: Value Stack in struts 2.

Explain Interceptors in struts 2.

Interceptors:
Interceptor is an object which is called at the pre processing and post processing of a request. These are same as the filters in the servlet.
Use of interceptors in struts 2.
1. Interceptors are used to perform some pre-processing task before the action is invoked.
2. Interceptors are used to perform some post-processing task after the action is invoked.
3. are used to perform operations like validation, internationalization etc
4. Interceptors can also be used to catch exceptions.

See more at: Interceptors in struts 2.

How to create custom interceptor in struts2?

We can create a custom interceptor in struts 2. To create a custom interceptor implements the Interceptor interface. Interceptor interface provides the methods to create a custom interceptor.

See more at: Struts 2 custom interceptor.

What are Result types in Struts?

As when request come business logic is executed first and after that result is to display as view. Struts provide a number of predefined result types. The dispatcher is the default result type. The tag is used to specify a result type in sturts.xml. The dispatcher, FreeMaker and redirect are the commonly used result types.

See more at: Struts 2 result type.

Explain struts2 validation framework.

Validation:
Validation is a process of checking something against a standard.
Struts 2 validation framework:
Struts 2 validation framework provides many generic built-in validation methods or validators to perform various validations. It enables the web container to perform validation rules before executing the actions. We can also create custom validators.
Ways of performing validations in struts 2.
1. By using built-in validators.
2. By using custom validators.

See more at: Struts 2 validation framework.

What are the types of validators in struts 2?

Types of validators in struts 2.
1. Field Validators.
2. Non-field validators.
1. Field validators:
These validators act or perform validations on the single field i.e. field validators are not generic they are specific to the single field. More than one validator can be applied to one field.
2. Non-field validators:
These validators are the action level validators. Single validator can be applied to multiple fields.
Note: In this approach only one validator can be applied to a single field.

See more at: Struts 2 validation framework.

Explain dynamic method invocation in struts2.

The dynamic method invocation is used to avoid the separate action mapping for every action in case of dispatch action functionality. We are using wildcard method to achieve dynamic method invocation.

See more at: Dynamic method invocation in struts2.

Explain DispatchAction Functionality in Struts 2.

The DispatchAction Functionality is a way of grouping the related actions into a single action class. In struts 1 DispatchAction class provides this functionality but in struts 2 every action class by default provide this functionality. To achieve this functionality in struts 2 we have to define the actions with the different names but having the same signature as execute method.

See more at: DispatchAction Functionality in Struts 2.

How tag libraries are defined in Struts?

Tag libraries are defined in the configuration file web.xml using tag.

<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

Is Struts thread safe?

Yes.