Struts2 Interview Questions and Answers
Page 1 of 4
What is the difference between Struts 1 vs Struts 2 ?
Which design pattern the Interceptors in Struts2 is based on ?
Interceptors in Struts2 are based on Intercepting Filters.
What are Pull-MVC and push-MVC based architecture ? Whicharchitecture does Struts2 follow ?
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.
Are Interceptors in Struts2 thread safe ?
No,Unlike Struts2 action, Interceptors are shared between requests, so thread issues will come if not taken care of.
Are Interceptors and Filters different ? , If yes then how ?
Apart from the fact that both Interceptors and filters are based on intercepting filter,there are few differences when it comes to Struts2.
Filters: (1)Based on Servlet Specification (2)Executes on the pattern matches on the request.(3) Not configurable method calls
Interceptors: (1)Based on Struts2. (2)Executes for all the request qualifies for a front controller( A Servlet filter ).And can be configured to execute additional interceptor for a particular action execution.(3)Methods in the Interceptors can be configured whether to execute or not by means of excludemethods or includeMethods. ( see tutorial on this Controlling Interceptor Behavior)
In Struts1, the front-controller was a Servlet but in Struts2, it is a filter. What is the possible reason to change it to a filter ?
There are two possibilities why filter is designated as front controller in Strtus2
(1)Servlet made as front controller needs developer to provide a right value in <load-on-startup> which lets the framework to initialize many important aspects( viz. struts configuration file)as the container starts.In absense of which the framework gets initialized only as the first request hits.Struts2 makes our life easy by providing front-controller as a filter,and by nature the filters in web.xml gets initialized automatically as the container starts.There is no need of such load-on-startup tag.
(2).The second but important one is , the introduction of Interceptors in Struts2 framework.It not just reduce our coding effort,but helps us write any code which we would have used filters for coding and necessary change in the web.xml as opposed to Struts1.So now any code that fits better in Filter can now moved to interceptors( which is more controllable than filters), all configuration can be controlled in struts.xml file, no need to touch the web.xml file.
(3).The front controller being a filter also helps towards the new feature of Struts ie UI Themes. All the static resources in the Themes now served through the filter
Which class is the front-controller in Struts2 ?
The class "org.apache.struts2.dispatcher.FilterDispatcher " is the front controller in Struts2. In recent time Struts 2.1.3 this class is deprecated and new classes are introduced to do the job. Refer: http://struts.apache.org/2.1.8/struts2-core/apidocs/org/apache/struts2/dispatcher/FilterDispatcher.html
What is the role of Action/ Model ?
Actions in Struts are POJO , is also considered as a Model. The role of Action are to execute business logic or delegate call to business logic by the means of action methods which is mapped to request and contains business data to be used by the view layer by means of setters and getters inside the Action class and finally helps the framework decide which result to render
How does Interceptors help achieve Struts2 a better framework than Struts1 ?
> Most of the trivial work are made easier to achieve for example automatic form population.
> Intelligent configuration and defaults for example you can have struts.xml or annotation based configuration and out of box interceptors can provide facilities that a common web application needs
>Now Struts2 can be used anywhere in desktop applications also, with minimal or no change of existing web application,since actions are now POJO.POJO actions are even easier to unit test.Thanks to interceptors
>Easier UI and validation in form of themes and well known DOJO framework.
>Highly plugable,Integrate other technologies like Spring,Hibernate etc at ease.
> Ready for next generation RESTFUL services
What is the relation between ValueStack and OGNL ?
A 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.