Struts 2
Walk me through the 10 step struts 2 request flow.
1. Request - Container - Filters incl ActionContextCleanup (optional ) and/else FilterDispatcher
2. FilterDispatcher consults ActionMapper
3. FilterDispatcher calls ActionProxy
4. ActionInvocation created by ActionProxy
5. Interceptors before
6. Action execute
7. Result and JSP
8. Interceptors after
9. Response returned through Filters
10. FilterDispatcher cleanup or ActionContextCleanup
1. Request goes to the Servlet container and is passed through a standard filter chain with the addition of ActionContextCleanup (optional) and FilterDispatcher.
2. FilterDispatcher consults ActionMapper to see if an Action is to be invoked.
3. If yes, FilterDispatcher delegates control to ActionProxy.
4. ActionProxy consults configuration and creates an ActionInvocation and initiates the Interceptor stack.
5. The before clause of each Interceptor is called until the execute method is called at the bottom of stack.
6. When the Action's execute method completes, the ActionInvocation references and executes the proper result according to configuration.
7. If appropriate, a template technology such as JSP is rendered including tags and urls for additional requests.
8. After clause of each interceptor is called (Reverse order since the calls are nested)
9. Response is returned through the Servlet filters.
10. Filter Dispatcher will not clean up ThreadLocal if ActionContextCleanup is present but does otherwise.
How does one create an Action in Struts 2?
Struts 2 uses reflection to find an appropriate method which by default is the execute method. Best practice however should be to implement the Action interface or extend ActionSupport.
What kind of MVC is Struts 2?
Struts 2 is a front controller MVC which is to say that the user interacts with the controller before the UI. You send a request and a controller or Action responds with the mapped UI components. When examined from the push/pull perspective, Struts2 is a Pull-MVC based architecture, in which all data is stored in Value Stack and retrieved by view layer for rendering.
In struts.xml, what does the attribute "method" stands for in the "action" tag?
The method attribute tells the name of the method invoked after setting the properties of the action. This attribute can hold the name of the method or the index of the result mapping.
For example:
<action method="login" name="login">
<result name="success">/success.jsp</result>
</action>
<action method="{1}" name="login">
<result name="login">/success.jsp</result>
</action>
In both, the method signature is
public String login()
Are Interceptors thread safe?
No, and they are supposed to be stateless according to the documentation.
Are Actions thread safe?
Struts 2 actions are but old Struts 1 actions were not. New struts actions are created per each request in Struts 2.
How are Interceptors and Servlet Filters different?
1. Filters are part of the Servlet API, Interceptors are Struts 2.
2. The Interceptor stack fires on requests in a configured package while filters only apply to their mapped URLs.
3. Interceptors can be configured to execute or not depending on specific target action methods via excludeMethods and includeMethods while Filters lack this feature.
4. Filters are an implementation of the Intercepting Filter pattern while Interceptors are of the Interceptor pattern.
How can duplicate form submission be handled in Struts 2?
The problem of duplicate form submission arises when a user clicks the Submit button more than once before the response is sent back. This may result in inconsistent transactions and must be avoided. In Struts this problem can be handled by using the saveToken() and isTokenValid() methods of Action class. saveToken() method creates a token (a unique string) and saves that in the user’s current session, while isTokenValid() checks if the token stored in the user’s current session is the same as that was passed as the request parameter.
What is struts.devMode and why it is used?
struts.devMode is a key used in struts.properties file or a constant configured instruts.xml file. It determines if the framework is running in development or production mode & is boolean. Dev mode gives the following benefits :
1. Resource bundle reload on every request; i.e. all localization properties file can be modified and the change will be reflected without restarting the server.
2. struts.xml or any configuration files can be modified without restarting or redeploying the application.
3. When errors occur in the application they will be displayed as stack traces, as oppose to production mode.
struts.devMode should be marked as false in production environment to reduce impact of performance. By default it is "false"
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. For configuring Spring, contextLoaderListener can be configured.
In struts.xml, what does the attribute "method" stands for in the "action" tag?
Ans: The method attribute tells the name of method to be invoked after setting the properties of the action class. This attribute can either hold the actual name of the method or the index of the result mapping.
For example:
<action method="login" name="login">
<result name="success">/success.jsp</result>
</action>
<action method="{1}" name="login">
<result name="login">/success.jsp</result>
</action>
In both the examples, the method being invoked by Struts will be login with the signature as
public String login()
If I have an ArrayList of say Employees class in the struts action (which may be fetched from the database), how can I iterate and display the Employee Id and Employee Name on the next JSP being shown? Assume the name of ArrayList to be employee
Ans: We can iterate such a collection as:
<s:iterator value="employee">
<s:property value="name"></s:property>
<s:property value="id"></s:property>
</s:iterator>
What is the advantage of extending the action class from ActionSupport class?
Ans: The use of ActionSupport class provides the features like validation, locale support and serialization to an action,
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 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 ?
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.
Can annotation-based and XML based configuration of actions coexists ?
Yes
What is the difference between empty default namespace and root name space ?
If the namespace attribute is not defined in the package tag or assigned "" value then it is called empty default namespace.While if "/" is assigned as value to the namespace attribute then it is called as root namespace.
The root namespace is treated as all other explicit namespaces and must be matched. It’s important to distinguish between the empty default namespace, which can catch all request patterns as long as the action name matches, and the root namespace, which is an actual namespace that must be matched.
Which interceptor is responsible for setting action's JavaBean properties ?
com.opensymphony.xwork2.interceptor.ParametersInterceptor is the interceptor class who sets the action's JavaBean properties from request.
What is the difference between Action and ActionSupport ?
Action is interface defines some string like SUCCESS,ERROR etc and an execute() method. For convenience Developer implement this interface to have access to String field in action methods. ActionSupport on other hand implements Action and some other interfaces and provides some feature like data validation and localized error messaging when extended in the action classes by developers.
How do you get the HttpServletRequest object in an interceptor ?
Here is the intercept method
public String intercept(ActionInvocation invoke) throws Exception {
ActionContext action=invoke.getInvocationContext();
HttpServletRequest req=(HttpServletRequest)action.get(StrutsStatics.HTTP_REQUEST);
return null;
}In the similar way you can get the response, by using StrutsStatics.HTTP_RESPONSE in get() method as above.
What is execute and wait interceptor ?
The ExecuteAndWaitInterceptor is great interceptor provided out of box in Struts2 for running long-lived actions in the background while showing the user a nice progress meter or a progress bar. For example while uploading a large file to the server we can use this interceptor to display a nice running progress bar instead of leaving the user in confusion that the application is not responding.This also prevents the HTTP request from timing out when the action takes more than 5 or 10 minutes.
Does the order in which interceptors execute matters ? If yes then why ?
Well, the answer is yes and no.Some Interceptors are designed to be independent so the order doesn't matter,but some interceptors are totally dependent on the previous interceptors execution.For example the validation and workflow interceptors,the validation interceptors checks if there is any error in the form being submitted to the action, then comes the workflow interceptor who checks if validation ( occured in the last) has any error,in presence of error it will not let the rest of the interceptors ( if any ) in the stack to execute.So this is very important that the validation interceptors execute first before the workflow. On the other hand lets say you wrote an interceptors who is doing the authentication and you have the user credential provided ( by the time this executes) it doesn't matter where this interceptors is placed( It is a different fact that you would like to keep it in the top ).
Who loads the struts.xml file ? Which Struts2 API loads the struts.xml file?
In Struts2 FilterServlet is the responsible class for loading struts.xml file as we deploy the application on the container.Unlike Servlet (as with Struts1) needs the load-on-startup tag to load the front controller,a filter doesn't need to have load on startup tag to be loaded as the application is deployed. As with servlet specification a filter is loaded/executed as the application starts up.
What is the difference between RequestAware and ServletRequestAware interface ?
RequestAware and ServletRequestAware both makes your action to deals with the servlet request, but in a difffrent ways,RequestAware gives you the attributes in the servlet request as a map( key as attribute name and value is the object added),But ServletRequestAware gives you the HttpServletRequest object itslef giving you more flexibility, with a price that ServletRequestAware makes your Action class too much tied to the Servlet environment making it dificult to unit test. So whenever a need to access only the attributes use the RequestAware interface.
What is the difference between EL and OGNL ?
OGNL is much like EL in JSPs,a language to traverse or manupulate objects like request , session or application in web context.OGNL stands for Object graph navigation language,which is used internally by Struts2, however we are not bound to use OGNL in our JSPs, we can use EL.But OGNL provides much more facilities than plain EL.For example while El interacts with the objects by means of getters/setters, OGNL supports whatever EL does along with lambda experssion, helps create functions on fly. OGNL has more flexible ways to deal with collection of objects.
What are the difference between ActionContext and ServletActionContext ?
ActionContext represents the context in which Action is executed, which itself doesn't hold any web parameters like session, request etc. ServletActionContext, extends the ActionContext and provides the web parameters to the Action.
What is the DispatchAction (Struts1) equivalent in Strtus2 ?
Struts1 provided the facility of having related action methods in a Single Action class,depending on the method parameter, the mapped methods were executed.To achieve this we have to extend the DispatchAction class in Struts1.But this comes as default in Struts2, We need to provide the qualified methods in the action class( no need to inherit any class), and provide the mapping of action path/name to the method attribute in action tag(struts.xml) and proper code in view layer.
What is the difference between DispatchAction and dynamic method invocation in Struts2 ?
How many different ways can you retrive the request parameters from within interceptor ?
Two ways.
In the first way retrieve the HttpServletRequest from the ActionContext object and retrieve the parameters the usual way.
ActionContext context=(ActionContext)invocation.getInvocationContext();
HttpServletRequest request=(HttpServletRequest)context.get(StrutsStatics.HTTP_REQUEST);
String username=(String)request.getParameter("user");
The second way is pretty much the Struts2, by invoking the getParameters method on context object. Code
ActionContext context=(ActionContext)invocation.getInvocationContext();
Map requestParameters=context.getParameters();
String usernames=((String[])m.get("user"))[0];
As you can see the map doesn't return the parameter as String unlike the Servlet specification, but an array of String (can be convinient for multivalued UI controls check box,single value UI controls data can be retrived as in above code ).
What is abstract package in Struts2, and what is its use ?
An abstract package usually defines inheritable components such as intercetpor,different interceptor stacks,result types etc.But doesn't contain any actions.The way we declare a package as abstract is through the package elements attribute as abstract and setting the value to "true". By default ( if abstract attribute is not mentioned) it is false.Struts-default.xml is an example of abstract package.
Does Struts2 mandates of implementing the Action interface in action classes to have the default Action method (execute)?
Struts2 doesn't mind if the action classes doesn't implement the Action interface, to have the execute method executed on default action method selection.Even though it appears that Action interfaces has the execute method declaration and one must implement it to have the execute method overriden in the action classes,Struts2 takes it as an informal contract with the developer by letting the developer to have an execute method conforming to the signature of execute method.Whenever Struts2 finds any voilation to the declaration of the method in unimplemented action class it throws the exeception.
1. Request - Container - Filters incl ActionContextCleanup (optional ) and/else FilterDispatcher
2. FilterDispatcher consults ActionMapper
3. FilterDispatcher calls ActionProxy
4. ActionInvocation created by ActionProxy
5. Interceptors before
6. Action execute
7. Result and JSP
8. Interceptors after
9. Response returned through Filters
10. FilterDispatcher cleanup or ActionContextCleanup
1. Request goes to the Servlet container and is passed through a standard filter chain with the addition of ActionContextCleanup (optional) and FilterDispatcher.
2. FilterDispatcher consults ActionMapper to see if an Action is to be invoked.
3. If yes, FilterDispatcher delegates control to ActionProxy.
4. ActionProxy consults configuration and creates an ActionInvocation and initiates the Interceptor stack.
5. The before clause of each Interceptor is called until the execute method is called at the bottom of stack.
6. When the Action's execute method completes, the ActionInvocation references and executes the proper result according to configuration.
7. If appropriate, a template technology such as JSP is rendered including tags and urls for additional requests.
8. After clause of each interceptor is called (Reverse order since the calls are nested)
9. Response is returned through the Servlet filters.
10. Filter Dispatcher will not clean up ThreadLocal if ActionContextCleanup is present but does otherwise.
How does one create an Action in Struts 2?
Struts 2 uses reflection to find an appropriate method which by default is the execute method. Best practice however should be to implement the Action interface or extend ActionSupport.
What kind of MVC is Struts 2?
Struts 2 is a front controller MVC which is to say that the user interacts with the controller before the UI. You send a request and a controller or Action responds with the mapped UI components. When examined from the push/pull perspective, Struts2 is a Pull-MVC based architecture, in which all data is stored in Value Stack and retrieved by view layer for rendering.
In struts.xml, what does the attribute "method" stands for in the "action" tag?
The method attribute tells the name of the method invoked after setting the properties of the action. This attribute can hold the name of the method or the index of the result mapping.
For example:
<action method="login" name="login">
<result name="success">/success.jsp</result>
</action>
<action method="{1}" name="login">
<result name="login">/success.jsp</result>
</action>
In both, the method signature is
public String login()
Are Interceptors thread safe?
No, and they are supposed to be stateless according to the documentation.
Are Actions thread safe?
Struts 2 actions are but old Struts 1 actions were not. New struts actions are created per each request in Struts 2.
How are Interceptors and Servlet Filters different?
1. Filters are part of the Servlet API, Interceptors are Struts 2.
2. The Interceptor stack fires on requests in a configured package while filters only apply to their mapped URLs.
3. Interceptors can be configured to execute or not depending on specific target action methods via excludeMethods and includeMethods while Filters lack this feature.
4. Filters are an implementation of the Intercepting Filter pattern while Interceptors are of the Interceptor pattern.
How can duplicate form submission be handled in Struts 2?
The problem of duplicate form submission arises when a user clicks the Submit button more than once before the response is sent back. This may result in inconsistent transactions and must be avoided. In Struts this problem can be handled by using the saveToken() and isTokenValid() methods of Action class. saveToken() method creates a token (a unique string) and saves that in the user’s current session, while isTokenValid() checks if the token stored in the user’s current session is the same as that was passed as the request parameter.
What is struts.devMode and why it is used?
struts.devMode is a key used in struts.properties file or a constant configured instruts.xml file. It determines if the framework is running in development or production mode & is boolean. Dev mode gives the following benefits :
1. Resource bundle reload on every request; i.e. all localization properties file can be modified and the change will be reflected without restarting the server.
2. struts.xml or any configuration files can be modified without restarting or redeploying the application.
3. When errors occur in the application they will be displayed as stack traces, as oppose to production mode.
struts.devMode should be marked as false in production environment to reduce impact of performance. By default it is "false"
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. For configuring Spring, contextLoaderListener can be configured.
In struts.xml, what does the attribute "method" stands for in the "action" tag?
Ans: The method attribute tells the name of method to be invoked after setting the properties of the action class. This attribute can either hold the actual name of the method or the index of the result mapping.
For example:
<action method="login" name="login">
<result name="success">/success.jsp</result>
</action>
<action method="{1}" name="login">
<result name="login">/success.jsp</result>
</action>
In both the examples, the method being invoked by Struts will be login with the signature as
public String login()
If I have an ArrayList of say Employees class in the struts action (which may be fetched from the database), how can I iterate and display the Employee Id and Employee Name on the next JSP being shown? Assume the name of ArrayList to be employee
Ans: We can iterate such a collection as:
<s:iterator value="employee">
<s:property value="name"></s:property>
<s:property value="id"></s:property>
</s:iterator>
What is the advantage of extending the action class from ActionSupport class?
Ans: The use of ActionSupport class provides the features like validation, locale support and serialization to an action,
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 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.
Can annotation-based and XML based configuration of actions coexists ?
Yes
What is the difference between empty default namespace and root name space ?
If the namespace attribute is not defined in the package tag or assigned "" value then it is called empty default namespace.While if "/" is assigned as value to the namespace attribute then it is called as root namespace.
The root namespace is treated as all other explicit namespaces and must be matched. It’s important to distinguish between the empty default namespace, which can catch all request patterns as long as the action name matches, and the root namespace, which is an actual namespace that must be matched.
Which interceptor is responsible for setting action's JavaBean properties ?
com.opensymphony.xwork2.interceptor.ParametersInterceptor is the interceptor class who sets the action's JavaBean properties from request.
What is the difference between Action and ActionSupport ?
Action is interface defines some string like SUCCESS,ERROR etc and an execute() method. For convenience Developer implement this interface to have access to String field in action methods. ActionSupport on other hand implements Action and some other interfaces and provides some feature like data validation and localized error messaging when extended in the action classes by developers.
How do you get the HttpServletRequest object in an interceptor ?
Here is the intercept method
public String intercept(ActionInvocation invoke) throws Exception {
ActionContext action=invoke.getInvocationContext();
HttpServletRequest req=(HttpServletRequest)action.get(StrutsStatics.HTTP_REQUEST);
return null;
}In the similar way you can get the response, by using StrutsStatics.HTTP_RESPONSE in get() method as above.
What is execute and wait interceptor ?
The ExecuteAndWaitInterceptor is great interceptor provided out of box in Struts2 for running long-lived actions in the background while showing the user a nice progress meter or a progress bar. For example while uploading a large file to the server we can use this interceptor to display a nice running progress bar instead of leaving the user in confusion that the application is not responding.This also prevents the HTTP request from timing out when the action takes more than 5 or 10 minutes.
Does the order in which interceptors execute matters ? If yes then why ?
Well, the answer is yes and no.Some Interceptors are designed to be independent so the order doesn't matter,but some interceptors are totally dependent on the previous interceptors execution.For example the validation and workflow interceptors,the validation interceptors checks if there is any error in the form being submitted to the action, then comes the workflow interceptor who checks if validation ( occured in the last) has any error,in presence of error it will not let the rest of the interceptors ( if any ) in the stack to execute.So this is very important that the validation interceptors execute first before the workflow. On the other hand lets say you wrote an interceptors who is doing the authentication and you have the user credential provided ( by the time this executes) it doesn't matter where this interceptors is placed( It is a different fact that you would like to keep it in the top ).
Who loads the struts.xml file ? Which Struts2 API loads the struts.xml file?
In Struts2 FilterServlet is the responsible class for loading struts.xml file as we deploy the application on the container.Unlike Servlet (as with Struts1) needs the load-on-startup tag to load the front controller,a filter doesn't need to have load on startup tag to be loaded as the application is deployed. As with servlet specification a filter is loaded/executed as the application starts up.
What is the difference between RequestAware and ServletRequestAware interface ?
RequestAware and ServletRequestAware both makes your action to deals with the servlet request, but in a difffrent ways,RequestAware gives you the attributes in the servlet request as a map( key as attribute name and value is the object added),But ServletRequestAware gives you the HttpServletRequest object itslef giving you more flexibility, with a price that ServletRequestAware makes your Action class too much tied to the Servlet environment making it dificult to unit test. So whenever a need to access only the attributes use the RequestAware interface.
What is the difference between EL and OGNL ?
OGNL is much like EL in JSPs,a language to traverse or manupulate objects like request , session or application in web context.OGNL stands for Object graph navigation language,which is used internally by Struts2, however we are not bound to use OGNL in our JSPs, we can use EL.But OGNL provides much more facilities than plain EL.For example while El interacts with the objects by means of getters/setters, OGNL supports whatever EL does along with lambda experssion, helps create functions on fly. OGNL has more flexible ways to deal with collection of objects.
What are the difference between ActionContext and ServletActionContext ?
ActionContext represents the context in which Action is executed, which itself doesn't hold any web parameters like session, request etc. ServletActionContext, extends the ActionContext and provides the web parameters to the Action.
What is the DispatchAction (Struts1) equivalent in Strtus2 ?
Struts1 provided the facility of having related action methods in a Single Action class,depending on the method parameter, the mapped methods were executed.To achieve this we have to extend the DispatchAction class in Struts1.But this comes as default in Struts2, We need to provide the qualified methods in the action class( no need to inherit any class), and provide the mapping of action path/name to the method attribute in action tag(struts.xml) and proper code in view layer.
What is the difference between DispatchAction and dynamic method invocation in Struts2 ?
How many different ways can you retrive the request parameters from within interceptor ?
Two ways.
In the first way retrieve the HttpServletRequest from the ActionContext object and retrieve the parameters the usual way.
ActionContext context=(ActionContext)invocation.getInvocationContext();
HttpServletRequest request=(HttpServletRequest)context.get(StrutsStatics.HTTP_REQUEST);
String username=(String)request.getParameter("user");
The second way is pretty much the Struts2, by invoking the getParameters method on context object. Code
ActionContext context=(ActionContext)invocation.getInvocationContext();
Map requestParameters=context.getParameters();
String usernames=((String[])m.get("user"))[0];
As you can see the map doesn't return the parameter as String unlike the Servlet specification, but an array of String (can be convinient for multivalued UI controls check box,single value UI controls data can be retrived as in above code ).
What is abstract package in Struts2, and what is its use ?
An abstract package usually defines inheritable components such as intercetpor,different interceptor stacks,result types etc.But doesn't contain any actions.The way we declare a package as abstract is through the package elements attribute as abstract and setting the value to "true". By default ( if abstract attribute is not mentioned) it is false.Struts-default.xml is an example of abstract package.
Does Struts2 mandates of implementing the Action interface in action classes to have the default Action method (execute)?
Struts2 doesn't mind if the action classes doesn't implement the Action interface, to have the execute method executed on default action method selection.Even though it appears that Action interfaces has the execute method declaration and one must implement it to have the execute method overriden in the action classes,Struts2 takes it as an informal contract with the developer by letting the developer to have an execute method conforming to the signature of execute method.Whenever Struts2 finds any voilation to the declaration of the method in unimplemented action class it throws the exeception.