Struts Interview Question Answers

Q1: What is struts2.0?

Struts 2.0 is a web application framework that implements the Model-View-Controller design pattern.Struts 1.0 merged with Webwork2.0 to get an optimal combination and form Struts2.0.
It Provides following features:
->Expression Language, OGNL
->Struts UI Tags
->Integration with AJAX,Spring Tiles etc.

Q2: Explain Struts MVC architecture?

Controller: FilterDispatcher-It inspect each incoming request to determine struts 2.0 action that should handle the request.It is specified in web.xml.
Model: Action classes-It can be simple java bean with execute method. It encapsulates calls to buisness logic into unit of work.It serves as a means of data transfer between request and result.
View: Result-It is presentation component which can be JSP,Freemarker,Velocity..etc. Action chooses result to rendr the response.

Q3: Explain struts request life cycle?

Request comes to server for some resource\processing. FilterDispatcher looks at the request and then determines the appropriate Action. Configured interceptors functionalities applies such as validation, file upload etc. Selected action is executed to perform the requested operation. Again, configured interceptors are applied to do any post-processing if required. Finally the result is prepared by the view and returns the result to the use.

Q4: What are different ways to create Action classes in Struts2?

Struts2 provide different ways to create action classes.
Any normal java class with execute() method returning String can be configured as Action class.
By implementing Action interface
Using Struts2 @Action annotation
By extending ActionSupport class

Q5: In struts.xml, what does the attribute "method" stands for in the "action" tag?

The method attribute tells the name of method to be invoked after setting the properties of the action class. This attribute can either hold the actual name of the method or the index of the result mapping.
For example:
<action class="" method="login" name="login">
<action class="" method="{1}" name="login">

Q6: Can annotation-based and XML based configuration of actions coexists ?


Q7: What is struts.devMode and why it is used ?

struts.devMode is a key used in file, to represent whether the framework is running in development mode or production mode by setting true or false. If set to development mode, it gives the following benefits : -
> Resource bundle reload on every request
> struts.xml or any configuration files can be modified without restarting or redeploying the application struts.devMode should be marked as false in production environment to reduce impact of performance. By default it is "false".

Q8: What are interceptors? How they are different from Filters?

Interceptors are similar to servlet filters and do preprocessing and post processing of request.
They are different from Filters:
Interceptors can be configured to execute additional interceptor for a particular action execution.
Methods in the Interceptors can be configured whether to execute or not by means of excludemethods or includeMethods

Q9: Are Interceptors in Struts2 thread safe?

No,Unlike Struts2 action, Interceptors are shared between requests, so thread issues will come if not taken care of.

Q10: What is the difference between Action and ActionSupport ?

Action is interface defines some string like SUCCESS,ERROR etc and an execute() method. For convenience Developer implement this interface to have access to String field in action methods. ActionSupport on other hand implements Action and some other interfaces and provides some feature like data validation and localized error messaging when extended in the action classes by developers.

Q11: What are struts application's configuration files?

Configuration files : web.xml, struts.xml, struts-config.xml and
web.xml: It is a deployment descriptor of a web application. It determines the controller of application:
struts.xml: It contains information regarding actions,results,global exceptions etc.
e.g: <struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello"
<result name="success">/HelloWorld.jsp</result>
struts-config.xml:struts-config.xml configuration file is a link between the View and Model components in the Web Client. configuration file provides a mechanism to change the default behavior of the framework.

Q12: What is default result type?

dispatcher result type is the default type.
<result name="success" type="dispatcher">
<param name="location">
</param >
Other option can be redirect,freemarker.

Q13: What is ValueStack?

Value stack is a set of several objects which keeps the following objects:
Model Object:If using model objects in your struts application, the current model object is placed before the action on the value stack
Action Object:current action object which is being executed.
Named Objects:#application, #session, #request, #attr and #parameter
We can use below code to get ValueStack:

Q14: What is OGNL?

Object-Graph Navigation Language (OGNL) is a powerful Expression Language that is used to manipulate data stored on the ValueStack. OGNL is based on a context and Struts builds an ActionContext map for use with OGNL.
The ActionContext map consists of the following:
application - application scoped variables
session - session scoped variables
request - request scoped variables
parameters - request parameters
atributes - the attributes stored in page, request, session and application scope
Data can be retrieved as following:
<s:property value="name"/>
<s:property value="#session.username"/>

Q15: How we can do validatoins in struts?

We can do validation in following way:
1)We can use validate method in action class. And for this make sure to extend ActionSupport class.
public void validate()
if (name == null || name.trim().equals(""))
addFieldError("name","The name is required");
2)XML based validaton: xml file needs to be created '[action-class]'-validation.xml:
<field name="name">
<field-validator type="required">
The name is required.

Q16: How to achieve Internationalization in struts?

Internationalization (i18n) is the process implementingservices so that they can easily be adapted to specific local languages.Struts2 provides localization ie. internationalization (i18n) support through resource bundles.Struts2 uses resource bundles to provide multiple language and locale options to the users of the web application.
Simplest naming format for a resource file is:
When you reference a message element by its key, Struts framework searches for a corresponding message bundle in the following order:

Access messages can be retrieved as:
1) <s:property value="getText('some.key')" />
2) <s:i18n name="some.package.bundle">
<s:text name="some.key" />
3) <s:textfield key="some.key" name="textfieldName"/>

Q17: What is life cycle of an interceptor?

Interceptor interface defines three methods – init(), destroy() and intercept().
init() method is called when interceptor instance is created and we can initialize any resources in this method.
destroy() method is called when application is shutting down and we can release any resources in this method.
intercept() is the method called every time client request comes through the interceptor.

Q18: Explain struts 2 Type Conversion?

Type Conversion is one of the feature available in struts using OGNL.All the data from form will get mapped using ODNL which do data transfer with type conversion.
We can write our own type converter by extending StrutsTypeConverter. And override convertFromString and convertToString method.

Q19: Exception Handling in struts?

Struts makes the exception handling easy by the use of the "exception" interceptor. To handle null pointer exception we can do configuration in xml file as:
<exception-mapping exception="java.lang.NullPointerException"
result="error" />
It can be done at action level or global level for all actions.

Q20: How we can achieve zero configuartion?

Zero configuration can be achieved using annotations. We can do action and results mapping using annotations.
For example:
@Result(name="success", location="/success.jsp"),
@Result(name="input", location="/index.jsp")
@RequiredFieldValidator( message = "The name is required" )

Q21: Explain struts 2 tags?

Struts 2 tags are categorised as:
1) Control tags: It can be if else,iterator,merge etc.
e.g: <s:if test="%{true}">
<div>True Condition</div>
2) Data tags: To manipulate data displayed on page.
e.g:date tag:
<s:date name="emp.birthday" format="dd/MM/yyyy" />
include tag:
<s:include value="jspPage.jsp" />
url tag:
<s:url value="editGadget.action">
<s:param name="id" value="%{selected}" />
3) Form\UI tags: To show form elements.
<s:text name="Please fill the form:" />
<s:form action="hello" method="post" enctype="multipart/form-data">
<s:textfield key="" name="name" />
<s:password key="person.password" name="password" />
<s:submit key="submit" />
4) Ajax Tags:Struts uses the DOJO framework for the AJAX tag implementation.
e.g: autocompleter:
<sx:autocompleter label="Colours"
list="{'Yellow','Green','Blue'}" />
<sx:datetimepicker name="BirthDate" label="Date Of Birth"
displayFormat="dd/MM/yyyy" />

Q22: What is the use of execAndWait interceptor?

Struts2 provides execAndWait interceptor for long running action classes. We can use this interceptor to return an intermediate response page to the client and once the processing is finished, final response is returned to the client.

Q23: What is struts-default package?

struts-default is an abstract package that defines all the Struts2 interceptors and commonly used interceptor stack. It is advisable to extend this package while configuring our application package to avoid configuring interceptors again.