Pages

Struts 2

Struts 2
Definition

Apache Struts 2 is an elegant, extensible framework for creating Enterprise Ready Java Web Applications (or) MVC Based Web Application. Apache Struts 2 Framework is designed to streamline the full development cycle from building, to deploying and maintaining applications over time. The Struts Framework was introduce by Craig McClanahan and donated to Apache Foundation in May, 2000 and Struts 1.0 was released in June 2001.

Struts2 is not just a new version of Struts 1, but it is a complete re-write of the Struts Architecture. Struts 2 is the combination of Web Work Framework of Open Symphony and Struts 1. Struts 2 is one of the best MVC Web Application Frameworks and being highly used for the development of Rich Internet Applications (RIA).

Struts 2 = Web Work + Struts 1


Features of Struts 2
Features Description
POJO Forms Struts 2 has done away with the Action Forms that were an integral part of the Struts Framework.
POJO Actions POJO Actions class are POJO. Here, you are not forced to implement any interface or inherit any class.
Tag Support Struts 2 has improved the form tags and the new tags which allow the developers to write less code.
AJAX Support Struts 2 provides support to AJAX Technology. AJAX is used to make asynchronous request i.e. it doesn't block the user. AJAX sends only required field data to the server side not all. So it makes the performance fast.
Easy Integration Integration with different type of Frameworks like Hibernate, Spring, Tiles, DWR, SiteMesh is now easier with a variety of integration available with Struts 2.
Plugin Support The Core Struts2 behavior can be enhanced and augmented by the use of plugins. A Number of plugins are available for Struts 2.
Template Support Struts 2 support for generating views using different type of templates.
View Technologies Struts 2 has a great support for multiple view options like JSP, Freemarker, Velocity and XSLT
Profiling Struts 2 offers integrated profiling to debug and profile the application.
Easy to Modify Tags Tag markups in Struts 2 can be tweaked using Freemarker templates. This does not require JSP or java knowledge. Basic HTML, XML and CSS knowledge is enough to modify the tags.
Promote Less configuration Struts 2 promotes less configuration with the help of using default values for various settings. You don't have to configure something unless it deviates from the default settings set by Struts 2.

Know About Struts 2 Framework
  1. Struts 2 is based on the OpenSymphony Web Works Framework.
  2. Struts 2 framework implements the Model-View-Controller (MVC) design pattern.
  3. Struts 2 the Model, View and Controller are implemented by the action, result and FilterDispatcher respectively.
  4. The controller‘s job is to map the user request to appropriate action.
  5. Struts 2 FilterDispatcher does the job of Controller.
  6. Model contains the data and the business logic.
  7. Struts 2 the model is implemented by the Action component.
  8. View is the presentation component of the MVC Pattern.
  9. The view is commonly implemented using JSP, Velocity Template, Freemaker or different Presentation Layer Technology.

Object Graph Navigation Language (OGNL) in Struts 2 Framework

OGNL stands for Object Graph Navigation Language. OGNL is an Expression Language. OGNL simplifies the accessibility of data stored in the ActionContext. The Struts 2 Framework sets the ValueStack as the root object of OGNL. Action object is pushed into the ValueStack. We can direct access the action property.


ActionSupport in Struts 2 Framework

ActionSupport is a Class. ActionSupport is a convenient class that implements many interfaces such as Action, Validateable, ValidationAware, TextProvider, LocaleProvider and Serializable. So it is mostly used instead of Action.

What is ConfigurationManagement ?

ConfigurationManagement is a Class. ConfigurationManagement Class know the suitable action for the request and the needed services for the request.

ActionInvocation in Struts 2 Framework

ActionInvocation is an Interface. ActionInvocation represents the execution state of an application. ActionInvocation holds the action and interceptors objects. The Struts 2 Framework provides ActionInvocation Interface to deal with ActionInvocation. ActionInvocation provides many methods, some of them can be used to get the instance of ValueStack, ActionProxy, ActionContext, Result.

Method Description
public void invoke() This Method using for invokes the next resource in processing this ActionInvocation.
public Action getAction() This Method returns the instance of Action associated with this ActionInvocation.
public Result getResult() This Method returns the instance of Result.
public ValueStack getStack() This Method returns the instance of ValueStack.
public ActionProxy getProxy() This Method returns the ActionProxy instance holding this ActionInvocation.
public ActionContext getInvocationContext() returns the ActionContext object associated with the ActionInvocation.

What is ActionProxy ?

ActionProxy is a class which is responsible to apply before and after services to original business logic.

What is ModelDriven ?

ModelDriven is used to transfer the form data into the object automatically.

What is Resource Bundle or Message Resource ?

If we want to get label and error message from an external file then we need to use resource bundle.

ActionContext in Struts 2 Framework

ActionContext is a Class. ActionContext is a static factory method. ActionContext is a container of objects in which action is executed. The values stored in the ActionContext are unique per thread. So, we do not need to make our action thread safe. We can get the reference of ActionContext by calling the getContext() method of ActionContext class.

ActionContext actionContext = ActionContext.getContext();

What is SessionAware ?

SessionAware is an interface. SessionAware must be implemented by the Action class to store the information in the sesion scope.

What is Interceptor ?

Interceptor is an object that is invoking at pre processing and post processing of a request.

Life Cycle Method of Interceptor :
  1. init () Method
  2. interceptor (ActionInvocation ai) Method
  3. destroy ()
What are the five constants of Action Interface ? Five Constants in Action Interface :
  1. NONE
  2. LOGIN
  3. INPUT
  4. ERROR
  5. SUCCESS
Struts 2 MVC Execution Flow Execution Flow :
  1. When a client request is given, a web container will receive request.
  2. Web Container loads web.xml file and verifies whether the url pattern are verified or not.
  3. If matches web container transfer the request to FilterDispatcher.
  4. FilterDispatcher hand over the request to ActionProxy.
  5. ActionProxy contacts ConfigurationManager Class.
  6. ConfigurtionManager Class loads struts.xml file and provides the required information back to ActionProxy.
  7. ActionProxy delegates the request along with its information to ActionInvocation.
  8. ActionInvocation executes the interceptors added to an action class.
  9. After that it will call the business logic.
  10. ActionInvocation receives finally result produced by an action class.
  11. ActionProxy transfers the result back to FilterDispatcher.
  12. FilterDispatcher selects an appropriate view based on the result.
Differentiate between Struts 1 and Struts 2 ?
Struts 1 Struts 2
Struts 1 front controller is ActionSupport. Struts 2 front controller is FilterDispatcher
Struts 1 have RequestProcessor. Struts 2 have Interceptors instead RequestProcessor.
Struts 1 have multiple tag libraries. Struts 2 do not have multiple tag libraries.
Struts 1 configuration file must be in web-inf folder. Struts 2 configuration file must be in source folder.
Struts 1 have programmatic and declarative validations. Struts 2 have annotation, programmatic and declarative validations.
Struts 1 action class object is not thread safe. Struts 2 action class object is thread safe.
Struts 1 supports only JSP. Struts 2 supports multiple view technologies.
Struts 1 have servlet dependency Struts 2 have does not have servlet dependency.
Struts 1 have Action Class. Struts 2 have Action Interface.
Differentiate between Spring and Struts Framework ?
Spring Framework Struts Framework
Spring is an application framework Struts is a web framework
Spring implements IOC Desgin Pattern Struts implements MVC Design Pattern
Spring can provides easy integration Struts can not provides easy integration
Spring is light weight Struts is heavy weight
Spring is layed architecture Struts is not a layed architecture
Spring is loosely coupled Struts is tightly coupled
Spring is a non-invarsive framework Struts is a invarsive framework

Difference between RequestAware and ServletRequestAware ?

RequestAware and ServletRequestAware both makes your action to deals with the Servlet request, but in a different 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 itself giving you more flexibility, with a price that ServletRequestAware makes your Action class too much tied to the Servlet environment making it difficult to unit test. So whenever a need to access only the attributes uses the RequestAware interface.

Difference between ActionContext and ServletActionContext ?

ActionContext represents the context in which Action is executed, which it doesn't hold any web parameters like session, request etc. ServletActionContext, extends the ActionContext and provides the web parameters to the Action.