Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Monday, December 29, 2008

STRUTS INTERVIEW QUESTIONS & ANSWERS

Q1.Describe Jakarta Struts Framework?
Ans:
Jakarta Struts Framework is implements Model-View-Controller design pattern , it is an open source code sponsored by Apache Jakarta Struts.It is server side implementation of MVC model-II. It saves time in design and implementation of a web based application due to this framework and pattern as I is highly robust, scalable and reliable.

Q2.What are the components of Struts?
Ans:
Struts is based on the MVC design pattern. Struts components can be categories into Model, View and Controller.
Model: Model is used to impalement business object. Components like business logic / business processes and data are the part of Model.
View: JSP, HTML etc. are part of View
Controller: ActionServlet of Struts is part of Controller components which works as front controller to handle all the requests.

Q3:What is ActionServlet ?
Ans:ActionServlet provides the "controller" in the Model-View-Controller (MVC) design pattern for web applications that is commonly known as "Model 2". It is backbone of struts application.It is used as controller to control .There can be only one ActionServlet defined for a context. ActionServlet is defined in web.xml file of a context e.g. WEB-INF/web.xml
Q4:What is an Action Class used for?

Ans:An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller (ActionServlet) will select an appropriate Action for each request, create an instance (if necessary), and call the perform method.
Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. In this means you should design with the following items in mind:
Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.
• Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary.
When an Action instance is first created, the controller servlet will call setServlet() with a non-null argument to identify the controller servlet instance to which this Action is attached. When the controller servlet is to be shut down (or restarted), the setServlet() method will be called with a null argument, which can be used to clean up any allocated resources in use by this Action.

Q5:What is ActionForm?
Ans:An ActionForm is a JavaBean optionally associated with one or more ActionMappings. Such a bean will have had its properties initialized from the corresponding request parameters before the corresonding action's perform() method is called.
When the properties of this bean have been populated, but before the perform() method of the action is called, this bean's validate() method will be called, which gives the bean a chance to verify that the properties submitted by the user are correct and valid. If this method finds problems, it returns an error messages object that encapsulates those problems, and the controller servlet will return control to the corresponding input form. Otherwise, the validate() method returns null(), indicating that everything is acceptable and the corresponding Action's perform() method should be called.
This class must be subclassed in order to be instantiated. Subclasses should provide property getter and setter methods for all of the bean properties they wish to expose, plus override any of the public or protected methods for which they wish to provide modified functionality.

Q6:What is Struts Validator Framework?
Ans: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.

Q7.What are the core classes of the Struts Framework?

Ans:Core classes of Struts Framework are ActionForm, Action, ActionMapping, ActionForward, ActionServlet etc.


Q8.What are Tag Libraries provided with Struts?

Ans: Struts provides a number of tag libraries that helps to create view components easily. These tag libraries are:
1) Bean Tags: Bean Tags are used to access the beans and their properties.
2) HTML Tags: HTML Tags provides tags for creating the view components like forms, buttons, etc..
3) Logic Tags: Logic Tags provides presentation logics that eliminate the need for scriptlets.
4) Nested Tags: Nested Tags helps to work with the nested context.

Q9:What are difference between ActionErrors and ActionMessage?

Ans:
ActionMessage:
A class that encapsulates messages is called ActionMessage. 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.

Q10:How you will handle exceptions in Struts?
Ans:
In Struts you can handle the exceptions in two ways:
1) Declarative Exception Handling: You can either define global exception handling tags in your struts-config.xml or define the exception handling tags within .. tag.
Example:
key="database.error.duplicate"
path="/UserExists.jsp"
type="mybank.account.DuplicateUserException"/>
2) Programmatic Exception Handling: Here you can use try{}catch{} block to handle the exception.


Q11:Give the Details of XML files used in Validator Framework?

Ans:The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validation-rules.xml is provided with the Validator Framework. The vlaidation-rules.xml is used to declares and assigns the logical names to the validation routines. It also contains the client-side java-script code for each validation routine. The validation routines are java methods plugged into the system to perform specific validations.

The Validator plug-in is supplied with a predefined set of commonly used validation rules such as Required, Minimum Length, Maximum length, Date Validation, Email Address validation and more. This basic set of rules can also be extended with custom validators if required.

The validation.xml configuration file defines which validation routines that is used to validate Form Beans. You can define validation logic for any number of Form Beans in this configuration file. Inside that definition, you specify the validations you want to apply to the Form Bean's fields. The definitions in this file use the logical names of Form Beans from the struts-config.xml file along with the logical names of validation routines from the validator-rules.xml file to tie the two together.

Example of form in the validation.xml file:








mask
^[0-9a-zA-Z]*$



Q12:How you will display validation fail errors on jsp page?
Ans:Following tag displays all the errors:



Q13:How you will enable front-end validation based on the xml in validation.xml?
Ans:The tag to allow front-end validation based on the xml in validation.xml. For example the code: generates the client side java script for the form "logonForm" as defined in the validation.xml file. The when added in the jsp file generates the client site validation script.


Q14:Can I setup Apache Struts to use multiple configuration files?
Ans:Yes.Struts can use multiple configuration files. Here is the configuration example:

banking
org.apache.struts.action.ActionServlet

config
/WEB-INF/struts-config.xml,
/WEB-INF/struts-authentication.xml,
/WEB-INF/struts-help.xml


1


Q15:How you will make available any Message Resources Definitions file to the Struts Framework Environment?
Ans: 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 tag.

Q16:What is Struts Flow?
Ans:
Struts Flow is a port of Cocoon's Control Flow to Struts to allow complex workflow, like multi-form wizards, to be easily implemented using continuations-capable JavaScript. It provides the ability to describe the order of Web pages that have to be sent to the client, at any given point in time in an application. The code is based on a proof-of-concept Dave Johnson put together to show how the Control Flow could be extracted from Cocoon.

Q17:What is LookupDispatchAction?
Ans: LookupDispatchAction
is an abstract Action that dispatches to the subclass mapped execute method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping.

Q18:What do you understand by DispatchAction?
Ans:
DispatchAction is an action that comes with Struts 1.1 or later, that lets you combine Struts actions into one class, each with their own method. The org.apache.struts.action.DispatchAction class allows multiple operation to mapped to the different functions in the same Action class.

Q19:Is struts threadsafe?Give an example?

Ans: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


Q20:What are the uses of tiles-def.xml file, resourcebundle.properties file, validation.xml file?

Ans:tiles-def.xml is an xml file used to configure tiles with the struts application. You can define the layout / header / footer / body content for your View.


Q21:What is the difference between perform() and execute() methods?
Ans:
Perform method is the method which was deprecated in the Struts Version 1.1. In Struts 1.x, Action.perform() is the method called by the ActionServlet. This is typically where your business logic resides, or at least the flow control to your JavaBeans and EJBs that handle your business logic. As we already mentioned, to support declarative exception handling, the method signature changed in perform. Now execute just throws Exception. Action.perform() is now deprecated; however, the Struts v1.1 ActionServlet is smart enough to know whether or not it should call perform or execute in the Action, depending on which one is available.

Q22:How Struts relates to J2EE?
Ans:Struts framework is built on J2EE technologies (JSP, Servlet, Taglibs), but it is itself not part of the J2EE standard.

Q23:What is Struts actions and action mappings?
Ans:A Struts action is an instance of a subclass of an Action class, which implements a portion of a Web application and whose perform or execute method returns a forward.
An action can perform tasks such as validating a user name and password.

An action mapping is a configuration file entry that, in general, associates an action name with an action. An action mapping can contain a reference to a form bean that the action can use, and can additionally define a list of local forwards that is visible only to this action.

An action servlet is a servlet that is started by the servlet container of a Web server to process a request that invokes an action. The servlet receives a forward from the action and asks the servlet container to pass the request to the forward's URL. An action servlet must be an instance of an org.apache.struts.action.ActionServlet class or of a subclass of that class. An action servlet is the primary component of the controller.

Q23:Can I have more than one struts-config.xml file?
Ans:Yes. We can have more than one struts-config.xml.A sample configuration in web.xml file would look like this


action
org.apache.struts.action.ActionServlet

config
/WEB-INF/struts-config.xml



config/exercise
/WEB-INF/exercise/struts-config.xml


config/upload
/WEB-INF/upload/struts-config.xml



Q24 What is Struts Validator Framework?
Ans: The Valuator Framework is used for server side validation in Struts Framework.The server side validation of form can be accomplised by sub class of our From Bean with DynaValidatorForm class.
Q25:What is the role of ActioMapping object in Struts Action class?
Ans: The role of the ActionMapping object to map a particular Action class.
Q26:What happens internally when actionMappings.findForward("target") method is called in Action class?
Ans:The actionMappings.findForward("target") method returns the object of ActionForward. An ActionForward represents a destination to which the controller, RequestProcessor,might be directed to perform a RequestDispatcher.forward or
HttpServletResponse.sendRedirect, as a result of processing activities of an Action class.

Q27 Expalin JSP,Servlet Combination VS Struts Which is better and how?
Ans:

Q28: Explain ActionForward against JSP:forward.
Ans:jsp:forward is part of JSTL and will be used in JSP pages. It internally uses RequestDispatcher to forward the request to new jsp or servlet page. ActionForward is part of Struts framework and is used in Action class. It internally uses RequestDispatcher to forward the request to ActionServlet.

What are difference between ActionErrors and ActionMessage?

Ans:ActionMessage: ActionMessage class encapsulates messages. Messages can be either global or specific to a particular bean property.Each individual message is described by an ActionMessage object, which contains a message key (to be looked up in an appropriate message resources database), 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 (and, therefore, a particular input field on the corresponding form).

What are the core classes of the Struts Framework?
Ans:
ActionForm, Action, ActionMapping, ActionForward, ActionServlet etc.

What is Struts Validator Framework?
Ans:
Struts Framework provides the functionality to validate the form data. It can be used for 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 validate Framework uses two xml files.First one is validation.xml and other one is validator-rules.xml .The validate frame work was developed by David Winterfelt as third party .But 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.

Give the Details of XML files used in Validator Framework?
Ans:
The Validator Framework uses two XML configuration files one is validator-rules.xml and second one validation.xml.The validator-rules.xml defines the standard validation routines. These routines are reusable and used in validation.xml to define the form specific validations. The validation.xml defines the validations applied to a form bean.

What are Tag Libraries provided with Struts?
Ans: Struts Frameworks
provides a number of tag libraries that helps to create view components in easy way.
These tag libraries are:
a) Bean Tags:
The bean Tags are used to access the beans and their properties.
b)HTML Tags:
HTML Tags provides tags for creating the view components like forms, buttons, etc.
c) Logic Tags:
Logic Tags provides presentation logics that eliminate the need for scriptlets.
d)Nested Tags:
Nested Tags helps to work with the nested context.

What are the components of Struts?
Ans:
Struts is based on the MVC design pattern. Struts components can be categories into Model, View and Controller.
Model:
Components like business logic / business processes and data are the part of Model. Java Bean is used .
View:
JSP, HTML etc. are part of View
Controller:
Controller is backbone of any struts application. ActionServlet of struts is part of Controller components which works as front controller to handle all the requests.

What is Struts actions and action mappings?
Ans:
A Struts action is an instance of a subclass of an Action class, which implements a portion of a Web application and whose perform or execute method returns a forward. An action can perform tasks such as validating a user name and password.
An action mapping is a configuration file entry that is associates an action name with an action. An action mapping can contain a reference to a form bean that the action can use, and can additionally define a list of local forwards that is visible only to this action.

What is MVC Architecture?
Ans:
MVC stands for Model ViewController. MVC pattern is a collaboration of three components model, view and controller.Model is backend database or java bean.View is output html or jsp.Controller is logic that is servlet.MVC pattern is a sequence of action interactions starting with view,then controller and then to model based on the data persistence. MVC is an approach for developing interactive application i.e. it results in events through user interactions.Model is responsible for holding the application state, View is for displaying the current model and controller handles the event..

How to get data from the velocity page in a action class?
Ans:
We can get the values in the action classes by using data.getParameter(”variable name defined in the velocity page”)

How you will enable front-end validation based on the xml in validation.xml?
Ans:
The tag to allow front-end validation based on the xml in validation.xml. For example the code: generates the client side javascript for the form \”logonForm\” as defined in the validation.xml file. The when added in the jsp file generates the client site validation script.

What is ActionForm?
Ans:
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.
What is Action class?
Ans:
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 passes the parameterized class to ActionForm 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.

What is ActionServlet?
Ans:
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.

How you will make available any Message Resources Definitions file to the Struts Framework Environment?
Ans:
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 tag. The message stored into message resources file is in key/value pairs.

: How you will make available any Message Resources Definitions file to the Struts Framework Environment?
Ans:
The Message Resources Definitions file is a 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 tag.
Example:

Q: Write code of any Action Class?
Ans:
Here is the code of Action Class that returns the ActionForward object.
ExampleAction.java
import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ExampleAction extends Action
{public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{

return mapping.findForward("testAction");
}
}

Q. How you will display validation fail errors on jsp page?
Ans:
Following tag displays all the errors:

Q. How you will enable front-end validation based on the xml in validation.xml?
Ans:
The tag to allow front-end validation based on the xml in validation.xml.
Example:
generates the client side java script for the form "loginForm" as defined in the validation.xml file. The when added in the jsp file generates the client site validation script.
Q: What is RequestProcessor and RequestDispatcher?
Ans:
The class org.apache.struts.action. RequestProcessor process the request from the controller. We can make a sublass of RequestProcessor with our own version and modify how the request is processed.
A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext() method is used to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.

Q: What are the uses of tiles-def.xml file, resourcebundle.properties file, validation.xml file?

Ans: tiles-def.xml: is is an xml file used to configure tiles with the struts application. You can define the layout / header / footer / body content for your View. See more at The resourcebundle.properties file is used to configure the message (error/ other messages) for the struts applications.
The file validation.xml is used to declare sets of validations that should be applied to FormBeans.

Q:What are the disadvantages of Struts?
Ans
: 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.

b) Harder to learn Struts are harder to learn, benchmark and optimize.
Q: What are the difference between and ?
Ans:
: This tag is used to output locale-specific text (from the properties files) from a MessageResources bundle.

: This tag is used to output property values from a bean. is a commonly used tag which enables the programmers to easily present the data.

: How you will make available any Message Resources Definitions file to the Struts Framework Environment?
Ans:
The Message Resources Definitions file is a 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 tag.
Example:

Q: Write code of any Action Class?
Ans:
Here is the code of Action Class that returns the ActionForward object.
ExampleAction.java
import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ExampleAction extends Action
{public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{

return mapping.findForward("testAction");
}
}

Q. How you will display validation fail errors on jsp page?
Ans:
Following tag displays all the errors:

Q. How you will enable front-end validation based on the xml in validation.xml?
Ans:
The tag to allow front-end validation based on the xml in validation.xml.
Example:
generates the client side java script for the form "loginForm" as defined in the validation.xml file. The when added in the jsp file generates the client site validation script.
Q: What is RequestProcessor and RequestDispatcher?
Ans:
The class org.apache.struts.action. RequestProcessor process the request from the controller. We can make a sublass of RequestProcessor with our own version and modify how the request is processed.
A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext() method is used to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.

Q: What are the uses of tiles-def.xml file, resourcebundle.properties file, validation.xml file?

Ans: tiles-def.xml: is is an xml file used to configure tiles with the struts application. You can define the layout / header / footer / body content for your View. See more at The resourcebundle.properties file is used to configure the message (error/ other messages) for the struts applications.
The file validation.xml is used to declare sets of validations that should be applied to FormBeans.

Q:What are the disadvantages of Struts?
Ans
: 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.

b) Harder to learn Struts are harder to learn, benchmark and optimize.
Q: What are the difference between and ?
Ans:
: This tag is used to output locale-specific text (from the properties files) from a MessageResources bundle.

: This tag is used to output property values from a bean. is a commonly used tag which enables the programmers to easily present the data.