Wednesday, December 31, 2008

Struts Interview Questions

Q: What is Struts Framework?

A: Struts is MVC architecture and The aim of MVC architecture is to separate the business logic and data of the application from the presentation of data to the user. Following is the small description of each of the components in MVC architecture:

Controller : All the user requests to the application go through the controller. The controller intercepts the requests from view and passes it to the model for appropriate action. Based on the result of the action on data, the controller directs the user to the subsequent view
Example :: Action Servlet

Model : The model represents the data of an application. Anything that an application will persist becomes a part of model. The model also defines the way of accessing this data ( the business logic of application) for manipulation. It knows nothing about the way the data will be displayed by the application. It just provides service to access the data and modify it.
Example :: Java Bean,EJB

View : The view represents the presentation of the application. The view queries the model for its content and renders it. The way the model will be rendered is defined by the view. The view is not dependent on data or application logic changes and remains same even if the business logic undergoes modification.
Example :: Jsp,Html

Q: What is Action Class and Write code of any Action Class?

A: The class org.apache.struts.action.ActionServlet is the called the ActionServlet which plays the role of controller. All the requests to the server goes through the controller. Controller is responsible for handling all the requests.and 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.

Here is the code of Action Class that returns the ActionForward object.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 TestAction extends Action
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
return mapping.findForward("testAction");

Q: How you will make available any Message Resources Definitions file to the Struts Framework Environment?

A: Message Resources Definitions file are simple .properties files .
Message Resources Definitions files can be added to the struts-config.xml file through message-resources tag.

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

Q: What is Struts Validator Framework and validation XML files ?
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.

The Validator Framework uses two XML configuration files

1) validator-rules.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 file is used to declare sets of validations that should be applied to Form Beans. Each Form Bean you want to validate has its own definition in this file.

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

Q.What are the various Struts tag libraries?
A)The various Struts tag libraries are:

Bean Tags
Logic Tags
Template Tags
Nested Tags
Tiles Tags

Q)Can we have more than one struts-config.xml file for a single Struts application?
A)Yes, application can have more than one struts-config.xml for a single Struts application. They can be included in param-value of web.xml.
Example :: /WEB-INF/struts-config.xml,/WEB-INF/struts-admin.xml,/WEB-INF/struts-config-forms.xml

Q)What is DynaActionForm and how do you create 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.

Using a DynaActionForm instead of a custom subclass of ActionForm is relatively straightforward.

In struts-config.xml: use your form-bean to be an org.apache.struts.action.DynaActionForm instead of some subclass of ActionForm

No comments:

Post a Comment

Your Ad Here ]]>