STRUTS INTERVIEW QUESTIONS

1: What is Struts?
A: The core of the Struts framework is a flexible control layer based on
standard technologies like Java Servlets, JavaBeans, ResourceBundles, and XML,
as well as various Jakarta Commons packages. Struts encourages application
architectures based on the Model 2 approach, a variation of the classic
Model-View-Controller (MVC) design paradigm.
Struts provides its own Controller component and integrates with other
technologies to provide the Model and the View. For the Model, Struts can
interact with standard data access technologies, like JDBC and EJB, as well as
most any third-party packages, like Hibernate, iBATIS, or Object Relational
Bridge. For the View, Struts works well with JavaServer Pages, including JSTL
and JSF, as well as Velocity Templates, XSLT, and other presentation systems.
The Struts framework provides the invisible underpinnings every professional
web application needs to survive. Struts helps you create an extensible
development environment for your application, based on published standards and
proven design patterns.

2: What is Jakarta Struts Framework?
A: 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.

3: 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.

4: How you will make available any Message Resources Definitions file to
the Struts Framework Environment?
A: T Message Resources Definitions file are simple .properties files and
these files contains the messages that can be used in the struts project.
Message Resources Definitions files can be added to the struts-config.xml file
through <message-resources /> tag.
Example:
<message-resources parameter=\"MessageResources\" />.

5: What is Action Class?
A: The Action Class is part of the Model and is a wrapper around the
business logic. 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. In the Action Class all the
database/business processing are done. It is advisable to perform all the
database related stuffs in the Action Class. The ActionServlet (commad) passes
the parameterized class to Action Form using the execute() method. 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.

6: What is ActionForm?
A: An ActionForm is a JavaBean that extends
org.apache.struts.action.ActionForm. ActionForm maintains the session state for
web application and the ActionForm object is automatically populated on the
server side with data entered from a form on the client side.



7: What is Struts Validator Framework?
A: 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.
The Validator framework was developed by David Winterfeldt as third-party
add-on to Struts. Now the Validator framework is a part of Jakarta Commons
project and it can be used with or without Struts. The Validator framework
comes integrated with the Struts Framework and can be used without doing any
extra settings.

8: Give the Details of XML files used in Validator Framework?
A: 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.

9: How you will display validation fail errors on jsp page?
A: Following tag displays all the errors:
<html:errors/>

10: How you will enable front-end validation based on the xml in
validation.xml?
A: 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.


11.Difference between MVC1 AND MVC2
Hi,MVC is a design pattern. It contains two models. MVC Model 1, MVC Model 2.Struts framework implements MVC Design Pattern. Struts can implement Model 1 and Model 2. Model 2 most properly describes the application of MVC in a Web-Application context.Following are the important feature of



MVC1 architecture: (1) HTML or JSP files are used to code the presentation. JSP files use java beans to retrieve data if required.(2)MVC1 architecture is page-centric design all the business and processing logic means any JSP page can either present in the JSP or may be called directly from the JSP page.(3)Data access is usually done using Custom tag or through java bean call.Therefore we can say that in

MVC1 there is tight coupling between page and model.

Following are the important feature of MVC2 architecture(1)This architecture removes the page-centric property of MVC1 architecture by separating Presentation, Control logic and Application state(2)In MVC2 architecture there is one Controller which receive all request for the application and is responsible for taking appropriate action in response to each request. Second one is Model which is represented by JavaBeans, business object, and database. Third one is View or is JSP page it takes the information provided by Controller and Module and presents it to user.Raj


e main difference b/w MVC-1 ans MVC-2 is MVC-1 is used for small scale applications and MVC-2 is used for large scale applications
in MVC-1 is used for small scale becoz the
whole req n response is controled by JSP
 but where as in MVC-2 request is taken by servlets n control is passed to JSP's or beans depending upon the request and response is given by JSP r any other view used.
There is nothing like MVC1 or MVC2. Sun specification talks
about Model - 1 and Model - 2 architecture. MVC or Model -
View - Controller architecture is equivalent to the Model -
2 architecture. In MVC the central idea is separation of
business logic and presentation logic, which was a
shortcoming of the Model - 1. You cannot have complex
business logic in your JSP, thats bad practice. In Struts
the ActionServlet is based on the FrontController Design
pattern. Ideally in MVC there are JSP pages to handle the
presentation logic, the model to encapsulate business data
and manipulation logic and a controller to co-ordinate
between the view and model.


MVC1 the controller is JSP where as in MVC2 the contrller is servlet


The main difference is MVC1 Combines both presentation
logic with the Business logic.MVC2 SEPARATE

In mvc1 we can have multiple controller servlets but in
mvc2 we can have only one controller servlet.For example
struts frame work is built on mvc2 architecture, which is
having only one controller i.e ActionSErvlet.


12.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>action</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-admin.xml,
     /WEB-INF/struts-config-forms.xml        
  </param-value>
</init-param>
.....
<servlet>




13.What is the difference between session scope and request scope when saving formbean ?
when the scope is request,the values of formbean would be available for the current request.
when the scope is session,the values of formbean would be available throughout the session.
14..What are the different kinds of actions in Struts?
The different kinds of actions in Struts are:
ForwardAction
IncludeAction
DispatchAction
LookupDispatchAction
SwitchAction

15.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.

16.How to use DispatchAction?
To use the DispatchAction, follow these steps :
Create a class that extends DispatchAction (instead of Action)
In a new class, add a method for every function you need to perform on the service – The method has the same signature as the execute() method of an Action class.
Do not override execute() method – Because DispatchAction class itself provides execute() method.
Add an entry to struts-config.xml

DispatchAction Example  »

17.What is the use of ForwardAction?
The ForwardAction class is useful when you’re trying to integrate Struts into an existing application that uses Servlets to perform business logic functions. You can use this class to take advantage of the Struts controller and its functionality, without having to rewrite the existing Servlets. Use 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.

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.

19.What is the difference between ForwardAction and IncludeAction?
The difference is that you need to use the IncludeAction only if the action is going to be included by another action or jsp. Use 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.

20.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.

21.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.