Wednesday, June 13, 2012

Struts 2 Interview Questions

1) How to create an action with Struts2?
 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?
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
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?
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?
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?
You can get the answer to that question from one my other blog post at:

7) How can one integrate Spring IoC with Struts 2?
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?
 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?
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?
This involves moving ActionForms of Struts1 to POJO properties of Struts 2.
Converting Struts1 struts-config.xml to struts.xml of Struts2. 
Rewriting the Struts action classes for Struts 2.


1 comment:

  1. What is the purpose of action tag in struts.xml? Please explain little about it.

    Start acquiring new patients at your clinic with the Podiatry website for your office.

    ReplyDelete