Saturday, 31 August 2013

Struts2 Best Interview Question in java

Struts2 framework is a combined work of webwork and Struts community. Previously it was known as webwork2 which later on named as Struts2. It uses Inversion Of Control (IoC) and architectural pattern MVC(Model-View-Controller), It also enablesAspect-Oriented Programming (AOP)usingInterceptors. As Struts2 has built in support of AJAX, it is also ideally suitable for developing web 2.0 application.
Model:Action
View:JSP / Velocity Template / Freemaker
Controller:StrutsPrepareandExecuteFilter
Struts2 is a pull-MVC framework. i.e. the data that is to be displayed to user has to be pulled from the Action.
Struts2 Architectural:It leverages the standard JEE technologies such as Java Filters, JavaBeans, ResourceBundles, Locales, XML etc in its architecture.

1)What is Struts?
Ans: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.

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 the advantage of having a POJO class as an action?
Ans: As mentioned in the first question, the POJO classis light weight and easy to test with frameworks like Junit. Moreover, there is no need to create separate Action Forms to hold the values from the source web page.

3) How can one integrate Spring IoC with Struts 2?
Ans: 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..


4)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 (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.

5)Mainclasses which are used instrutsapplication?
Ans:List of Main Classes
Action servlet: it’sa back-bone of web application it’s a controller class responsible for handling the entire request.
Action Class: using Action classes all the business logic is developed us call model of the application also.
Action Form: it’s a java bean which represents our forms and associated with action mapping. And it also maintains the session state its object is automatically populated onthe server side with data entered  from a form on the client side.
Action Mapping: using this class we do the mapping between object and Action.

ActionForward: this class in Struts is used to forward the result from controller to destination

6)What are Pull-MVC and push-MVC based architecture ?Which architecture does Struts2 follow ?
Ans:Pull-MVC and Push-MVC are better understood with how the view layer is getting data i.e.Model to render. In case of Push-MVC the data(Model) is constructed and given to the view layer by the Controllers by putting it in the scoped variables like request or session. Typical example is Spring MVC and Struts1. Pull-MVC on the other hand puts the model data typically constructed in Controllers are kept in a common place i.e. in actions, which then gets rendered by view layer.Struts2 is a Pull-MVC based architecture, in which all data is stored in Value Stack and retrieved by view layer for rendering

7)Are Interceptors in Struts2 thread safe ?
Ans:No, This is not Thread Safe, Interceptors are shared between requests, so thread issues will come if not taken care of.

8)What is the relation between ValueStack and OGNL ?
Ans: ValueStack is a place where all the data related to action and the action itself is stored. OGNL is a mean through which the data in the ValueStack is manipulated.



9) How Will Run the Execute Method in Action Class?

Ans Action Servelet will run the execute method in Struts,as it is the Main controller of Strus



10) Why We Use Resource Bundle in Struts2?

Ans:To use resource bundle to retrieve the message from properties file,