Struts 2 Interview Questions


1) How to create an action with Struts2?
Ans: Creating an action in Struts2 is very different from Struts1 in the sense that there is no mandatory requirement to extend a class from the struts library. A Java bean/POJO which has the private member variables and public getters and setters is sufficient to become a struts action class. As far as execute() method of Struts1 is concerned, a method with return type of String is sufficient. The method name is to be specified in the struts.xml. This change is done so that the testing of struts based application can be done easily.

2) In struts.xml, what does the attribute "method" stands for in the "action" tag?
Ans: 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="com.company.app.Login" method="login" name="login">
    <result name="success">/success.jsp</result>
</action>
<action class="com.company.app.Login" method="{1}" name="login">
    <result name="login">/success.jsp</result>
</action>
In both the examples, the method being invoked by Struts will be login with the signature as 
public String login()


3) If I have an ArrayList of say Employees class in the struts action (which may be fetched from the database), how can I iterate and display the Employee Id and Employee Name on the next JSP being shown? Assume the name of ArrayList to be employee
Ans: We can iterate such a collection as:

<s:iterator value="employee">
       <s:property value="name"></s:property>
      <s:property value="id"></s:property>
</s:iterator>

4) What is the advantage of having a POJO class as an action?
Ans: As mentioned in the first question, the POJO class is light weight and easy to test with frameworks like Junit. Moreover, there is no need to create separate ActionForms to hold the values from the source web page.

5) What is the advantage of extending the action class from ActionSupport class?
Ans: The use of ActionSupport class provides the features like validation, locale support and serialization to an action,

6) How can we get rid of .action at the end of URL's in struts 2?
Ans: 


7) How can one integrate Spring IoC with Struts 2?
Ans:  Struts 2 comes with support for Spring and Spring can be used to inject beans to various classes. It can also be used to inject properties to the action class of struts 2. For configuring Spring, contextLoaderListener can be configured.

8) Describe the flow of a request in a Struts 2 web application?
Ans: It can be best understood by using a diagram. Please refer the following URL for understanding the same.

9) What tool/IDE/frameworks do you use to write code in a Struts 2 web application?
Ans: Mostly it is MyEclipse 9 which has excellent support for struts 2. Netbeans 7  has support for Struts 1 but not Struts 2. Other that the IDE one can also mention tools like DreamWeaver, Spring, Hibernate, XMLSpy etc.

10) What are the steps to migrate a web application written with Struts 1 to Struts 2?
Ans: This involves moving ActionForms of Struts1 to POJO properties of Struts 2.
Converting Struts1 struts-config.xml to struts.xml of Struts2. 


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email