Quick Refresh : Struts2 : Flow, ActionMapper, ConfigurationManager, ActionContext, ActionInvocation, ValueStack

Struts2 

Struts 2 Flow:


The flow of the struts 2 application, is combined with many components such as Controller, ActionProxy, ActionMapper, Configuration Manager, ActionInvocation, Interceptor, Action, Result, etc.

  1. A user sends a request for the action
  2. Container maps the request in the web.xml file and gets the class name of the controller.
  3. Container invokes the controller (StrutsPrepareAndExecuteFilter or FilterDispatcher). Since struts2.1, it is StrutsPrepareAndExecuteFilter. Before 2.1 it was FilterDispatcher.
  4. Controller gets the information for the action from the ActionMapper. means FilterDispatcher in turn uses the ActionMapper to determine weather to invoke an Action or not. If the action is required to be invoked, the FilterDispatcher delegates the control to the ActionProxy.
  5. Controller invokes the ActionProxy
  6. ActionProxy gets the information of action and interceptor stack from the configuration manager which gets the information from the struts.xml file.
  7. Then the ActionProxy creates an ActionInvocation, which implements the command pattern. ActionProxy forwards the request to the ActionInvocation
  8. ActionInvocation invokes each interceptors and action
  9. A result is generated
  10. The result is sent back to the ActionInvocation
  11. A HttpServletResponse is generated
  12. The response is sent to the user






Struts 2 ActionMapper: 
The ActionMapper interface provides a mapping between HTTP requests and action invocation requests and vice-versa. When given an HttpServletRequest, the ActionMapper may return null if no action invocation request matches, or it may return an ActionMapping that describes an action invocation for the framework to try.


DefaultActionMapper - By default, the DefaultActionMapper is used. Default action mapper implementation, using the standard *.[ext] (where ext usually "action") pattern. The extension is looked up from the Struts configuration key struts.action.extension.
ConfigurationManager: This is the java object representation for the struts.xml file which is loaded at the application startup and contains all the information regarding framework configuration. The ConfigurationProvider interface describes the framework's configuration. By default, the framework loads its configurations via an xml document by using the StrutsXmlConfigurationProvider.
Struts 2 ActionContext: The ActionContext is a container of objects in which action is executed. The values stored in the ActionContext are unique per thread (i.e. ThreadLocal). So we don't need to make our action thread safe.
We can get the reference of ActionContext by calling the getContext() method of ActionContext class. It is a static factory method. 
ActionContext context = ActionContext.getContext();

Struts 2 ActionInvocation: ActionInvocation represents the execution state of an action. It holds the action and interceptors objects.
ActionInvocation Interface: The struts framework provides ActionInvocation interface to deal with ActionInvocation. It provides many methods, some of them can be used to get the instance of ValueStack, ActionProxy, ActionContext, Result etc.

Methods of ActionInvocation Interface- The commonly used methods of ActionInvocation interface are as follows:
1)public ActionContext getInvocationContext() returns the ActionContext object associated with the ActionInvocation.
2)public ActionProxy getProxy() returns the ActionProxy instance holding this ActionInvocation.
3)public ValueStack getStack() returns the instance of ValueStack.
4)public Action getAction() returns the instance of Action associated with this ActionInvocation.
5)public void invoke() invokes the next resource in processing this ActionInvocation.
6)public Result getResult() returns the instance of Result.

Struts 2 ValueStack: A valueStack is simply a stack that contains application specific objects such as action objects and other model object. At the execution time, action is placed on the top of the stack. We can put objects in the valuestack, query it and delete it. 
ValueStack Interface:  The struts 2 framework provides an interface to deal with valuestack. 

Methods of ValueStack interface: There are many methods in ValueStack interface. The commonly used methods are as follows:
  1. public String findString(String expr) finds the string by evaluating the given expression.
  2. public Object findValue(String expr) finds the value by evaluating the specified expression.
  3. public Object findValue(String expr, Class c) finds the value by evaluating the specified expression.
  4. public Object peek() It returns the object located on the top of the stack.
  5. public Object pop() It returns the object located on the top of the stack and removes it.
  6. public void push(Object o) It puts the object on the top of the stack.
  7. public void set(String key, Object value) It sets the object on the stack with the given key. It can be get by calling the findValue(key) method.
  8. public int size() It returns the number of objects from the stack.

 ObjectFactory: Although ObjectFactory is not shown in above diagram, still this is one of the core important class of the framework. All objects created by the framework are instantiated by the ObjectFactory. The ObjectFactory provides the means of integrating the framework with IoC containers like Spring. 

Comments

Popular posts from this blog

Ramoji Film City, Hyderabad, India

Ashtavinayak Temples | 8 Ganpati Temples of Maharashtra | Details Travel Reviews

Quick Refresh : Hibernate