Sunday, October 2, 2011

Struts Interview Questions and Answers(All in one)

Q.1)What is Jakarta Struts Framework?
Jakarta Struts is open source implementation of MVC (Model-View-Controller) pattern for the development of web based applications. Jakarta Struts is robust architecture and can be used for the development of application of any size. Struts framework makes it much easier to design scalable, reliable Web applications with Java.

Q.2)What is MVC ?
The main aim of the MVC architecture  is to separate the business logic and application data from the presentation data to the user.
1).  Model: The model contains the core of the application's functionality. The model encapsulates the state of the application. Sometimes the only functionality it contains is state. It knows nothing about the view or controller.
2). View :The view provides the presentation of the model. It is the look of the application. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing about the controller. The view should be notified when changes to the model occur.
3). Controller:  Whenever the user sends a request for something then it always go through the controller. The controller is responsible for intercepting the requests from view and passes it to the model for the appropriate action. After the action has been taken on the data, the controller is responsible for directing the appropriate view to the user. In  GUIs, the views and the controllers often work very closely together.

Q.3)Why we should use the MVC design pattern?
They are resuable : When the problems recurs, there is no need to invent a new solution, we just have to follow the pattern and adapt it as necessary.
They are expressive: By using the MVC design pattern our application becomes more expressive.

Q.4)What is ActionServlet?
A: The class org.apache.struts.action.ActionServlet is the called the ActionServlet. In the the Jakarta Struts Framework this class plays the role of controller. All the requests to the server goes through the controller. Controller is responsible for handling all the requests.

Q.5)What is role of ActionServlet?

ActionServlet performs the role of Controller:
1)Process user requests
2)Determine what the user is trying to achieve according to the request
3)Pull data from the model (if necessary) to be given to the appropriate view,
4)Select the proper view to respond to the user
5)Delegates most of this grunt work to Action classes
6)Is responsible for initialization and clean-up of resources

Q.6)What is Action Class?

The Action is part of the controller. The purpose of Action Class is to translate the HttpServletRequest to the business logic. To use the Action, we need to  Subclass and overwrite the execute()  method. The ActionServlet passes the parameterized class to Action Form using the execute() method. There should be no database interactions in the action. The action should receive the request, call business objects.  The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the file as per the value of the returned ActionForward object.

Q.7)What is the ActionForm?
ActionForm is javabean which represents the form inputs containing the request parameters from the View referencing the Action bean.

Q.8)What is the life cycle of ActionForm?
The lifecycle of ActionForm invoked by the RequestProcessor is as follows:
1)Retrieve or Create Form Bean associated with Action
2)"Store" FormBean in appropriate scope (request or session)
3)Reset the properties of the FormBean
4)Populate the properties of the FormBean
5)Validate the properties of the FormBean
6)Pass FormBean to Action

Q.9)What is the signature of execute() method?
public ActionForward execute(
ActionMapping mapping,
ActionForm form,HttpServletRequest request,
HttpServletResponse response)
 throws Exception ;

Q.10)What is Struts Validator Framework?
Struts Framework provides the functionality to validate the form data. It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits the java scripts and it can be used validate the form data on the client browser. Server side validation of form can be accomplished by sub classing your From Bean with DynaValidatorForm class.

Q.11)What design patterns are used in Struts?
1)Service to Worker
2)Dispatcher View
3)Composite View (Struts Tiles)
4)Front Controller
5)View Helper
6)Synchronizer Token

Q12)Give the Details of XML files used in Validator Framework?

The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validator-rules.xml defines the standard validation routines, these are reusable and used in validation.xml. to define the form specific validations. The validation.xml defines the validations applied to a form bean.

Q.13)How you will display validation fail errors on jsp page?
Use <html:errors/>

Q.14) How you will enable front-end validation based on the xml in validation.xml?
The <html:javascript> tag to allow front-end validation based on the xml in validation.xml.
For  example the code: <html:javascript formName="logonForm" dynamicJavascript="true" staticJavascript="true" /> generates the client side java script for the form "logonForm" as defined in the validation.xml file. The <html:javascript> when added in the jsp file generates the client site validation script.

Q.15)What are the different kinds of actions in Struts?
The different kinds of actions in Struts are:
ForwardAction
IncludeAction
DispatchAction
LookupDispatchAction
SwitchAction

Q.16)What is DispatchAction?
The DispatchAction class is used to group related actions into one class. Using this class, you can have a method for each logical action compared than a single execute method. The DispatchAction dispatches to one of the logical actions represented by the methods. It picks a method to invoke based on an incoming request parameter. The value of the incoming parameter is the name of the method that the DispatchAction will invoke.

Q.17)What is the use of ForwardAction?

ForwardAction to forward a request to another resource in your application, such as a Servlet that already does business logic processing or even another JSP page. By using this predefined action, you don’t have to write your own Action class.You just have to set up the struts-config file properly to use ForwardAction.
 
Q.18)What is IncludeAction?
The IncludeAction class is useful when you want to integrate Struts into an application that uses Servlets. Use the IncludeAction class to include another resource in the response to the request being processed.

Q.19)What is LookupDispatchAction?
The LookupDispatchAction is a subclass of DispatchAction. It does a reverse lookup on the resource bundle to get the key and then gets the method whose name is associated with the key into the Resource Bundle.

Q.20)What is the use of LookupDispatchAction?
LookupDispatchAction is useful if the method name in the Action is not driven by its name in the front end, but by the Locale independent key into the resource bundle. Since the key is always the same, the LookupDispatchAction shields your application from the side effects of I18N.

Q.21)What is difference between LookupDispatchAction and DispatchAction?
The difference between LookupDispatchAction and DispatchAction is that the actual method that gets called in LookupDispatchAction is based on a lookup of a key value instead of specifying the method name directly.

Q.22)What is SwitchAction?
The SwitchAction class provides a means to switch from a resource in one module to another resource in a different module. SwitchAction is useful only if you have multiple modules in your Struts application. The SwitchAction class can be used as is, without extending.

Q.23)What are the various Struts tag libraries?

Struts is very rich framework and it provides very good and user friendly way to develop web application forms. Struts provide many tag libraries to ease the development of web applications. These tag libraries are:
1)Bean tag library - Tags for accessing JavaBeans and their properties.
2)HTML tag library - Tags to output standard HTML, including forms, text boxes, checkboxes, radio buttons etc..
3)Logic tag library - Tags for generating conditional output, iteration capabilities and flow management
4)Tiles or Template tag library - For the application using tiles
5)Nested tag library - For using the nested beans in the application
6)Template

Q.24)Can we have more than one struts-config.xml file for a single Struts application?

Yes, we can have more than one struts-config.xml for a single Struts application. They can be configured as follows:
<servlet>
  <servlet-name>banking</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet
  </servlet-class>
  <init-param>
  <param-name>config</param-name>
  <param-value>/WEB-INF/struts-config.xml,
  /WEB-INF/struts-authentication.xml,
  /WEB-INF/struts-help.xml
   </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

Q.25)What are the core classes of the Struts Framework?
Core classes of Struts Framework are ActionForm, Action, ActionMapping, ActionForward, ActionServlet etc.

Q.26)What is DynaActionForm?
A specialized subclass of ActionForm that allows the creation of form beans with dynamic sets of properties (configured in configuration file), without requiring the developer to create a Java class for each type of form bean.

Q.27)How the exceptions are handled in struts?
Struts handle exception in two ways:-
Programmatic exception handling :
Explicit try/catch blocks in any code that can throw exception. It works well when custom value (i.e., of variable) needed when error occurs.
Declarative exception handling :You can either define <global-exceptions> handling tags in your struts-config.xml or define the exception handling tags within <action></action> tag. It works well when custom page needed when error occurs. This approach applies only to exceptions thrown by Actions.

Q.28)What are differences between <bean:message> and <bean:write>?
<bean:message>: is used to retrive keyed values from resource bundle. It also supports the ability to include parameters that can be substituted for defined placeholders in the retrieved string.
<bean:message key="prompt.customer.firstname"/>

<bean:write>: is used to retrieve and print the value of the bean property. <bean:write> has no body.
<bean:write name="customer" property="firstName"/>

Q.29)What are difference between ActionErrors and ActionMessage?
ActionMessage: A class that encapsulates messages. Messages can be either global or they are specific to a particular bean property.Each individual message is described by an ActionMessage object, which contains a message key,and up to four placeholder arguments used for parametric substitution in the resulting message.
ActionErrors: A class that encapsulates the error messages being reported by the validate() method of an ActionForm. Validation errors are either global to the entire ActionForm bean they are associated with, or they are specific to a particular bean property

Q.30)Is struts threadsafe?Give an example?
Struts is not only thread-safe but thread-dependant. The response to a request is handled by a light-weight Action object, rather than an individual servlet. Struts instantiates each Action class once, and allows other requests to be threaded through the original object. This core strategy conserves resources and provides the best possible throughput. A properly-designed application will exploit this further by routing related operations through a single Action

Q.31)What are the disadvantages of Struts?
Struts is very robust framework and is being used extensively in the industry. But there are some disadvantages of the Struts:
a) High Learning Curve
Struts requires lot of efforts to learn and master it. For any small project less experience developers could spend more time on learning the Struts.
Harder to learn
Struts are harder to learn, benchmark and optimize.

Q.32)How Struts relates to J2EE?

Struts framework  is built on J2EE technologies (JSP, Servlet, Taglibs), but it is itself not part of the J2EE standard.

Q.33)What if <action> element has <forward> declaration with same name as global forward?
In this case the global forward is not used. Instead the <action> element’s <forward> takes precendence.

No comments:

Post a Comment